简体   繁体   English

将 @Autowired 与 Jersey + Spring 框架一起使用时出现 NullPointerException

[英]NullPointerException while using @Autowired with Jersey + Spring frameworks

I have created a simple service to try out Jersey and Spring framework together.我创建了一个简单的服务来一起试用 Jersey 和 Spring 框架。 I am using core Spring (Not Spring Boot).我正在使用核心 Spring(不是 Spring Boot)。 Jersey framework works fine without Spring. Jersey 框架在没有 Spring 的情况下也能正常工作。 Once I add Spring annotations, @Autowired is not working as expected.添加 Spring 注释后,@Autowired 无法按预期工作。

MyResource.java我的资源

@Component
@Path("myresource")
public class MyResource {

    @Autowired
    private AdditionService add;

    @GET
    @Path("/sum/{firstNumber}/{secondNumber}")
    @Produces(MediaType.APPLICATION_JSON)
    public Answer getSum(@PathParam("firstNumber") int firstNumber,
                         @PathParam("secondNumber") int secondNumber) {

        return add.performAction(firstNumber,secondNumber);
    }
}

AdditionService.java添加服务.java

@Component
public class AdditionService {

    @Autowired
    Answer ans;

    public Answer performAction (int first, int second) {
        ans.setAnswer(first+second);
        return ans;
    }
}

Answer.java答案.java

@Component
public class Answer {
    private int answer = 200;
    public Answer() {}
    public Answer(int answer) {
        this.answer = answer;
    }
    public int getAnswer() {
        return answer;
    }
    public void setAnswer(int answer) {
        this.answer = answer;
    }
}

AppConfig.java应用程序配置文件

@Configuration
@ComponentScan(basePackageClasses = {Answer.class,AdditionService.class})
public class AppConfig {}

Errors错误

org.apache.catalina.core.NamingContextListener.addResource naming.jmxRegistrationFailed

NullPointer error空指针错误

24-Feb-2020 16:54:30.205 SEVERE [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Jersey Web Application] in context with path [/RESTAPIJersey_war] threw exception [java.lang.NullPointerException] with root cause
    java.lang.NullPointerException
        at org.example.company.resources.MyResource.getSum(MyResource.java:27)
        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)

这些应该被注释为@Service而不是@Component,然后您将从一个或多个@Controllers(甚至@Restcontrollers)访问这些服务,这些@Controllers继承@Component Annotation,您可以在其中指定@RequestMapping(method=RequestMethod.GET) , path="/users") 方法类级别

Can you try removing @ComponentScan annotation from AppConfig and use lije below您可以尝试从 AppConfig 中删除 @ComponentScan 注释并在下面使用 lije

@SpringBootApplication(scanBasePackages = "com.example")
@EnableAutoConfiguration
public class AppConfig {
}

Also ensure your AppConfig class should be discoverable (you can put AppConfig in com.example) and try还要确保您的 AppConfig 类应该是可发现的(您可以将 AppConfig 放在 com.example 中)并尝试

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

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