0

I am working on JAX-RS web services but constantly getting an NPE for spring as it is not able to Autowire the class.

This is how I have defined it in the applicationContext:

<context:annotation-config />
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.geidea.spring.service"></context:component-scan>
<context:component-scan base-package="com.geidea.web.rest"></context:component-scan>

<bean id="terminalDao" class="com.geidea.spring.dao.TerminalDaoImpl" />
<bean id="terminalServices" class="com.geidea.spring.service.TerminalServiceImpl" />

This is my rest Service:

package com.geidea.web.rest;
@Component
@Path(value = "/")
public class TerminalRestController{

    private final Logger log = LoggerFactory.getLogger(TerminalRestController.class);


    @Autowired
    private TerminalService terminalService;


    public TerminalService getTerminalService() {
        return terminalService;
    }

    public void setTerminalService(TerminalService terminalService) {
        this.terminalService = terminalService;
    }


    @POST
    @Path("/processTerminals")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultMessage processTerminals(TerminalsData data){

        log.debug("Processing Terminals Web Service");
        System.out.println(data.getTerminalsData().getCurrencyCode().getCurrencyCode()+ " "+data.getTerminalsData().getCurrencyCode().getAlphaCode());
        ResultMessage response = new ResultMessage();
        response.setMessage("Some Error Occurred");
        response.setResponseCode("-1");

        List<Term> existingTerminals = new ArrayList<Term>();
        existingTerminals = terminalService.fetchTerminals();

I am getting error on this line "existingTerminals = terminalService.fetchTerminals();"

The error is:

    11:47:19,721 DEBUG [rest.TerminalRestController -64] Processing Terminals Web Service
13-Jan-2020 11:47:19.725 SEVERE [http-nio-8080-exec-1] com.sun.jersey.spi.container.ContainerResponse.mapMappableContainerException The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
        java.lang.NullPointerException
                at com.geidea.web.rest.TerminalRestController.processTerminals(TerminalRestController.java:71)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
                at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)

Thanks in advance.

Talib
  • 1,134
  • 5
  • 31
  • 58
  • 1
    The `TerminalRestController` is managed by JAX-RS not by Spring, so adding `@Autowired` nor `@Component` won't help. You currently have 2 instances. Setup proper JAX-RS and Spring integration as explained in the JAX-RS manual. – M. Deinum Jan 13 '20 at 08:50
  • 1
    This is a Jersey controller, not a Spring controller. Why are you using both (and why are you using old-style Spring configuration)? – chrylis -cautiouslyoptimistic- Jan 13 '20 at 08:50
  • If you are using Spring Boot, then why applicationContext.xml? – MariuszS Jan 13 '20 at 08:51
  • Hi Guys, sorry I am using Jersey for the first time. What do you guys suggest? @M.Deinum – Talib Jan 13 '20 at 08:51

0 Answers0