java - @Autowired 给出空指针异常

标签 java spring-mvc spring-boot autowired spring-jersey

我已经完成了@Autowired注释,并且@Component注释正在调用并调用类。尽管如此,我在“ServiceResource.java”中遇到空指针异常 -> serviceValidator 值为空。

请帮我知道我哪里出了问题?

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 生命周期和作用域实例管理进行任何集成,因此 Spring 不会以任何方式处理 @Autowired 注释。

您必须在 Jersey 中设置 Spring 集成和支持,为此,您可以将 Jersey Spring 扩展依赖项添加到项目配置中。

如果您使用 Maven,对于 Jersey 最高版本 2.25,您必须添加以下依赖项:

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

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

这样,Jersey 端点将作为 Spring bean 进行管理,从而通过 @Autowired 注释启用依赖项注入(inject)。

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

我认为面对这种配置问题的最佳方法是使用 Spring Boot,并且在 Holon Platform 中,我管理的开源项目,我们提供了许多自动配置工具来将 Spring 框架与 Jersey 结合使用(例如,Spring JAX-RS beans 自动注册为 Jersey 资源,从而摆脱了 jersey.config.server.provider.packages 等配置参数)。

您可以查看this example on GitHub了解如何仅使用 Spring Boot starter 来设置带有 Spring 集成的 Jersey 服务器。

关于java - @Autowired 给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037266/

相关文章:

spring - 链接 Spring View 解析器

spring-boot - 在 spring boot/spring data elasticsearch 中禁用自动索引创建

java - 将数据从 Java 类插入数据库

java - 每次调用方法时如何将 1 添加到另一个类的整数?

java - Spring Security 和 Spring Social 外部身份验证

javascript - 如何使用 Spring MVC 和 ThymeLeaf 正确验证复选框?

spring-boot - 如何将base64转换为java中的MultipartFile

java - Spring Boot 2.0 - 禁用连接到数据库

java - 堆数组 - 删除根

java - <<=运算符在Java中是什么意思?