NPE весной DI в веб-службе Джерси - PullRequest
0 голосов
/ 13 января 2020

Я пытаюсь интегрировать Spring DI в веб-сервис JAX-RS, но у объекта DI есть ошибка ниже:

13-Jan-2020 14:03:26.862 SEVERE [http-nio-8080-exec-8] 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:66)
            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)
            at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
            at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
            at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
            at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
            at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
            at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
            at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
            at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
            at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
            at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
            at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
            at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
            at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

web. xml выглядит следующим образом:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5" metadata-complete="true">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
     <servlet>
    <servlet-name>TerminalBatchUpload</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.geidea.web.rest</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.geidea.web.rest</param-value>
</init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>
  <servlet-mapping>
    <servlet-name>TerminalBatchUpload</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
     <servlet-name>Faces Servlet</servlet-name>
     <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
</web-app>

Мой класс веб-сервисов:

    Component
@Path(value = "/")
public class TerminalRestController{

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

    @Autowired
    private TerminalService terminalServices;


    public TerminalService getTerminalServices() {
        return terminalServices;
    }

    public void setTerminalServices(TerminalService terminalServices) {
        this.terminalServices = terminalServices;
    }




    @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 = terminalServices.fetchTerminals();

applicationContext выглядит следующим образом:

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- Enable Spring Annotation Configuration -->
    <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>

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

    <!-- Create Data Source bean -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
        <property name="url" value="jdbc:jtds:sqlserver://127.0.0.1:1433/realtime-empty;useNTLMv2=true;domain=mydomain.local;" />

    </bean>
    <!-- Define SessionFactory bean -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>domain-classes.hbm.xml</value>
            </list>
        </property>
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>
    <!-- Transaction Manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- Detect @Transactional Annotation -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

Получение ошибки в этой строке существующиеTerminals = TerminalServices.fetchTerminals ();, Пожалуйста помоги. Я не уверен, что мне здесь не хватает.

1 Ответ

0 голосов
/ 13 января 2020

Ваш путь TerminalRestController должен находиться в контексте: component-scan.

, AutowiredAnnotationBeanPostProcessor и CommonAnnotationBeanPostProcessor неявно включаются при использовании элемента component-scan. Это означает, что оба компонента автоматически обнаруживаются и соединяются вместе - и все без метаданных конфигурации бина, предоставленных в XML.

Сканирование пути к классам и управляемые компоненты

...