繁体   English   中英

@Autowired提供空指针异常

[英]@Autowired giving null pointer exception

我已经完成了@Autowired批注,并且@Component批注正在调用和称为类。 不过,我在“ ServiceResource.java”-> serviceValidator值中将Null指针异常设置为null。

请帮助我知道我要去哪里错了?

ServiceResouce.java

package com.resolvebug.app.controller;
@Component
@Path("v1")
@Produces(APPLICATION_JSON)
public class ServiceResource {
    @Autowired
    private ServiceValidator serviceValidator;

    @POST
    @Path("/post/json")
    public ServiceManager employeeLogin(ServiceManager serviceManager) {
        return serviceValidator.hello(serviceManager);
    }
}

ServiceValidator.java

package com.resolvebug.app.validator;
@Component
public class ServiceValidator {
    @Autowired
    private ServiceManager sm;

    public ServiceManager hello(ServiceManager serviceManager) {
        sm.setAge(serviceManager.getAge());
        sm.setName(serviceManager.getName());
        return sm;
    }
}

ServiceManager.java

package com.resolvebug.app.handler;
@Component
public class ServiceManager {
    public String name;
    public int age;
    public ServiceManager() {
    }
    public ServiceManager(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>REST Web Application using Jersey Web Application</display-name>

    <servlet>
        <servlet-name>rest-server</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.resolvebug.app.controller</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>rest-server</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

应用环境

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <context:spring-configured />
    <context:annotation-config />
    <context:component-scan
        base-package="com.resolvebug.app.controller" />
    <mvc:annotation-driven />

</beans>

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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.resolvebug.app</groupId>
    <artifactId>bk-services</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>bk-services</name>

    <properties>
        <jdk_version>1.8</jdk_version>
        <jersey_version>2.22.1</jersey_version>
        <servlet_version>3.1.0</servlet_version>
        <junit_version>3.8.1</junit_version>
        <springbeans_version>3.0.5.RELEASE</springbeans_version>
        <springcontext_version>3.0.2.RELEASE</springcontext_version>
        <springweb_version>5.0.5.RELEASE</springweb_version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet_version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit_version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springbeans_version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springcontext_version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springweb_version}</version>
        </dependency>


        <!-- Jersey -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey_version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey_version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>bk-services</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk_version}</source>
                    <target>${jdk_version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

您的项目配置如何? Spring和Jersey之间的集成无法立即使用,因为Jersey资源端点的生命周期并不了解Spring Bean实例的创建和配置。

默认情况下,Jersey在每次请求时都会创建一个新的端点资源实例,而没有与Spring bean lifecyle和作用域实例管理进行任何集成,因此@Autowired批注不会以任何方式由Spring处理。

您必须在Jersey中设置Spring集成和支持,然后才能将Jersey Spring扩展依赖项添加到项目配置中。

如果使用Maven,则对于泽西版2.25之前的版本,必须添加以下依赖项:

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.25</version>
</dependency>

对于2.26版或更高版本的Jersey,可以使用jersey-spring4

这样,Jersey端点将作为Spring bean管理,从而通过@Autowired批注启用依赖项注入。

还要确保正确设置了Spring配置(包括应用程序上下文初始化和bean注册阶段)。

我认为解决此配置问题的最佳方法是使用Spring Boot,在Holon Platform (我管理的开源项目)中,我们提供了许多自动配置工具,可将Spring框架与Jersey一起使用(例如,Spring JAX -RS Bean自动注册为Jersey资源,摆脱了jersey.config.server.provider.packages之类的配置参数。

您可以在GitHub上查看此示例,以了解如何仅使用Spring Boot启动来设置具有Spring集成的Jersey服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM