在jersey资源中使用setter方法注入后,object被设置为null

时间:2017-02-09 07:28:17

标签: spring tomcat jersey

我使用spring 4进行依赖设置器注入,使用jersey 2进行restful web服务。当我在tomcat上运行这个项目时,会注入该对象,但是当我使用注入依赖项调用服务层类方法时,我得到的是NullPointerException.Here是泽西资源。在这方面提供帮助。

package com.mobileware.fitransxt.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.mobileware.fitransxt.authentication.request.AuthenticationRequest;
import com.mobileware.fitransxt.authentication.response.AuthenticationResponse;
import com.mobileware.fitransxt.boimpl.AuthenticationImpl;

import com.mobileware.fitransxt.responsebuilder.Response;
import com.mobileware.fitransxt.responsebuilder.ResponseBuilder;

@Path("/auth")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class AuthenticationResource {

    private AuthenticationImpl objAuthentication;




    public AuthenticationImpl getObjAuthentication() {
        return objAuthentication;
    }




    public void setObjAuthentication(AuthenticationImpl objAuthentication) {
        System.out.println(objAuthentication);
        this.objAuthentication = objAuthentication;
        System.out.println(this.objAuthentication);
    }




    @Path("/authentication")
    @POST
    public Response authenticate(AuthenticationRequest objRequest) {

        System.out.println(objAuthentication);
        System.out.println("auth");
        AuthenticationResponse objResponse = objAuthentication.authenticate(objRequest);
        return ResponseBuilder.buildSuccessResponse(objResponse);
    }

}

我的Web.xml

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- Spring Listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Jersey Servlet -->
    <servlet>
        <servlet-name>FITransXT</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- Register resources and providers -->
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.mobileware.fitransxt.resource</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>FITransXT</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!-- loading Spring Context for registering beans with ApplicationContext -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/FITransXT-servlet.xml</param-value>
    </context-param>



</web-app> 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.mobileware.fitransxt</groupId>
    <artifactId>FITransXT</artifactId>
    <version>0.0.1</version>
  </parent>
  <artifactId>FITransXT-Impl</artifactId>
  <dependencies>
  <dependency>
   <groupId>com.mobileware.fitransxt</groupId>
 <artifactId>FITransXT-Type</artifactId>
 <version>0.0.1</version>
 </dependency> 
 <dependency>
 <groupId>com.mobileware.fitransxt</groupId>
 <artifactId>FITransXT-BOImpl</artifactId>
 <version>0.0.1</version>
 </dependency>
 <!--  <dependency>
 <groupId>com.mobileware.fitransxt</groupId>
 <artifactId>FITransXT-DaoImpl</artifactId>
 <version>0.0.1</version>
 </dependency> -->
    <!-- Jersey core Servlet 2.x implementation -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>${jersey.version}</version>
          <!-- <scope>compile</scope>  -->
    </dependency>

    <!-- Jersey JSON Jackson (2.x) entity providers support module -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
        <!-- <scope>${jersey.scope}</scope> -->
    </dependency>

    <!-- Jersey extension module providing support for Spring 3 integration -->
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>${jersey.version}</version>
       <!--  <scope>${jersey.scope}</scope> -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </exclusion>
             <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Spring Framework-4.x -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.1.RELEASE</spring.version>
        <jersey.version>2.12</jersey.version>
        <slf4j.version>1.7.2</slf4j.version>
        <jdk.source.version>1.8</jdk.source.version>
        <jdk.target.version>1.8</jdk.target.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project> 

0 个答案:

没有答案