我已经完成了@Autowired 注释,并且@Component 注释正在调用和调用类。尽管如此,我在“ServiceResource.java”-> serviceValidator 值中得到空指针异常为空。
请帮助我知道我哪里出错了?
服务资源.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);
}
}
服务验证器.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;
}
}
服务管理器.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>