Jersey 和 Spring 集成 - bean 注入在运行时为 null

发布于 2024-11-19 21:22:13 字数 1994 浏览 2 评论 0原文

我正在尝试将服务注入到使用 Jersey 的 Rest 类中。

无论我尝试什么或如何注入此类,似乎在运行时都显示为 null。查看日志文件显示,当 Web 应用程序初始化时,setJsonTestService 正在被调用,并且此时它不为 null。但是,当稍后通过对该类的 PUT 请求访问它时,它为空。

我完全困惑了。

课程看起来像这样:

@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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2024-11-26 21:22:14

尝试一下

<servlet-class>
     com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>

您还需要 contextConfigLocation ,但我假设您已经拥有它。 请参阅此处了解有关设置的更多详细信息

正如 matt b 建议 spring 实例化您的对象,但 Jersey不知道有关 spring 的任何信息并再次实例化它们本身。当您使用 SpringServlet 时,它应该找到 spring 应用程序上下文。

也就是说,spring-mvc 提供对 RESTful 服务的支持,这与 JAX-RS 非常相似。你也可以尝试一下。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文