[英]Jersey and spring integration - bean Injections are null at runtime
我正在尝试将服务注入使用 Jersey 的 Rest class 中。
无论我尝试如何或如何尝试注入此 class 似乎在运行时显示为 null 。 查看日志文件显示,当 web 应用程序初始化时正在调用 setJsonTestService,此时它不是 null。 但是,稍后通过对这个 class 的 PUT 请求访问它时,它是 null。
我完全感到困惑。
class 看起来像这样:
@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...");
}
}
}
有任何想法吗?
更新:
web.xml(球衣)
<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>
尝试
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
您还需要contextConfigLocation
<context-param>
,但我假设您拥有它。 有关设置的更多详细信息,请参见此处
正如 matt b 建议的那样, spring 实例化您的对象,但 Jersey 对 spring 一无所知并再次实例化它们。 当您使用SpringServlet
时,它应该找到 spring 应用程序上下文。
也就是说,spring-mvc 提供了与 JAX-RS 非常相似的 RESTful 服务支持。 你也可以试试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.