Unable to access CDI

I have a class that contains a method annotated with @PostConstruct which fetchs list of customers

    @ViewScoped
    @Named
 public class CustomerController extends AlertResponse {
  @PostConstruct
    public List<Customer> init() {
    ResponseEntity<List<Customer>> listResponse =
            restTemplate.exchange("http://localhost:8030/api/customers/listCustomers",
                    HttpMethod.GET, null, new ParameterizedTypeReference<>() {
                    });
    customersList = listResponse.getBody();
    return customersList;
}

When i run the application i get this error

java.lang.IllegalStateException: Unable to access CDI
at javax.enterprise.inject.spi.CDI.current(CDI.java:65)
at com.sun.faces.cdi.CdiUtils.createDataModel(CdiUtils.java:308)
at com.sun.faces.facelets.component.UIRepeat.getDataModel(UIRepeat.java:255)
at com.sun.faces.facelets.component.UIRepeat.setIndex(UIRepeat.java:465)
at com.sun.faces.facelets.component.UIRepeat.process(UIRepeat.java:515)
at com.sun.faces.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:1081)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1648)
at org.primefaces.component.column.Column.renderChildren(Column.java:90)
at org.primefaces.component.datatable.DataTableRenderer.encodeCell(DataTableRenderer.java:1356)
at org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:1272)
at org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:1179)
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:1119)
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:1072)
at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:401)
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:331)
at org.primefaces.component.datatable.DataTableRenderer.render(DataTableRenderer.java:106)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:97)

When i removed these lines, the view is rendred fine

           <p:column headerText="Customer Status" sortBy="#{customer.customerStatus}"
                          filterBy="#{customer.customerStatus}">
                    <ui:repeat value="#{customer}" var="data">
                                                <h:outputText rendered="#{data.customerStatus == 'Unlocked'}">
                                                    <p:tag styleClass="p-mr-2" severity="success" value="UnLocked"
                                                           rounded="true"/>
                                                </h:outputText>
                                                <h:outputText rendered="#{data.customerStatus == 'Locked'}">
                                                    <p:tag styleClass="p-mr-2" severity="danger" value="Locked"
                                                           rounded="true"/>

                                                </h:outputText>
                                            </ui:repeat>
                </p:column>

What could be the problem Thank you in advance

1 answer

  • answered 2022-05-06 23:37 user3192295

    @PostConstruct is for initialization purposes like opening a database connection. Methods annotated with @PostConstruct must have void return type and no parameter:

    void init() { ...
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum