Jersey and spring integration - bean Injections are null at runtime(Jersey 和 spring 集成 - bean Injections 在运行时为空)
问题描述
I am trying to inject services into a Rest class that is using Jersey.
No matter what or how I try to inject into this class seems to be showing up as null at runtime. Looking in the log files shows that the setJsonTestService is being called when the web app is initialized and that it isn't null at this point. But, when access it later with a PUT request to this class, it is null.
I am completely baffled.
The class looks like this:
@Named
@Path("JsonTest")
public class JsonTest {
@Context
Request request;
@Context
UriInfo uriInfo;
protected final Logger log = Logger.getLogger(getClass());
private JsonTestService jsonTestService;
@Autowired
public void setJsonTestSerivce(JsonTestService jsonTestService) {
log.info("Setting JsonTestService.");
if (jsonTestService == null) {
log.info("JsonTestService is null at injection");
}
this.jsonTestService = jsonTestService;
}
@Inject
public ScrapIntermediate scrapIntermediate; // just a plain empty class with an is true method
@PUT
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public void putJson(@PathParam("id") String id) {
log.info("Putting some json at " + id);
if (scrapIntermediate == null) {
log.info("scrapIntermediate is null...");
}
if (jsonTestService != null) {
jsonTestService.sendUpdate();
} else {
log.info("jsonTestService is null...");
}
}
}
Any ideas?
Update:
web.xml (Jersey)
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>EDAS</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Try
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
You will also need the contextConfigLocation
<context-param>
, but I assume you have it. See here for more details about the setup
As matt b suggested spring instantiates your objects, but Jersey does not know anything about spring and instantiates them itself again. When you use the SpringServlet
it should locate the spring application context.
That said, spring-mvc provides support for RESTful services that is very similar to that of JAX-RS. You can try it as well.
这篇关于Jersey 和 spring 集成 - bean Injections 在运行时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jersey 和 spring 集成 - bean Injections 在运行时为空
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01