简体   繁体   English

jersey-spring3 Bridge找不到我的资源

[英]jersey-spring3 Bridge Isn't Finding My Resources

I'm using the Jersey / Spring bridge ( https://jersey.java.net/documentation/latest/spring.html ) and I can't get it to see my Jersey resources via a Spring XML config file. 我正在使用Jersey / Spring桥( https://jersey.java.net/documentation/latest/spring.html ),但无法通过Spring XML配置文件查看我的Jersey资源。

(Spring 3.2, Jersey 2.5, jersey-spring3 2.5, jackson-jaxrs-json-provider 2.2.3.) (Spring 3.2,Jersey 2.5,jersey-spring3 2.5,jackson-jaxrs-json-provider 2.2.3)

In my Spring config file I have 在我的Spring配置文件中

<context:component-scan base-package="com.fasterxml.jackson.jaxrs.json, com.mycompany.mappers, com.mycompany.resources" />

SpringComponentProvider.initialize() succeeds, and SpringComponentProvider.bind gets called for a bunch of classes (well, actually just the stuff in the Jersey server's WADL package), but is not called for my resource classes. SpringComponentProvider.initialize()成功, SpringComponentProvider.bind被调用了一堆类(好吧,实际上只是泽西服务器的WADL包中的东西),但我的资源类没有被调用。

I can see that Spring is finding my resource: 我可以看到Spring正在寻找我的资源:

2013-12-30 16:47:24,246 [main]  org.springframework.beans.factory.support.DefaultSingletonBeanRegistry:215  DEBUG   Creating shared instance of singleton bean 'myResource'
2013-12-30 16:47:24,246 [main]  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory:432    DEBUG   Creating instance of bean 'myResource'

The sample app referenced in the docs ( https://github.com/jersey/jersey/tree/2.5/examples/helloworld-spring-webapp ) defines a Jersey application subclass of org.glassfish.jersey.server.ResourceConfig and programmatically registers resources. 文档( https://github.com/jersey/jersey/tree/2.5/examples/helloworld-spring-webapp )中引用的示例应用程序定义了org.glassfish.jersey.server.ResourceConfig的Jersey应用程序子类,并以编程方式注册资源。 I was hoping to use a Spring XML config file to bind resources, rather than hardcoding them. 我希望使用Spring XML配置文件来绑定资源,而不是对其进行硬编码。 (This used to work in Jersey 1.x.) (这曾经在Jersey 1.x中起作用。)

How can I get Jersey 2 to recognize my resources? 如何获得Jersey 2来识别我的资源?

I'll answer my own question. 我会回答我自己的问题。

The answer is that the Spring bridge for Jersey 2 doesn't support <context:component-scan .../> and you have to register your resources programmatically. 答案是,Jersey 2的Spring桥不支持<context:component-scan .../> ,您必须以编程方式注册资源。

Have a look at the Jersey/Spring docs ( https://jersey.java.net/documentation/latest/spring.html ) and follow the Spring sample app exactly ( https://github.com/jersey/jersey/tree/master/examples/helloworld-spring-webapp )... if you step off that path then it won't work. 看一下Jersey / Spring文档( https://jersey.java.net/documentation/latest/spring.html )并完全按照Spring示例应用程序( https://github.com/jersey/jersey/tree/ master / examples / helloworld-spring-webapp )...如果您退出该路径,则它将无法正常工作。

The key part is to subclass ResourceConfig (eg https://github.com/jersey/jersey/blob/master/examples/helloworld-spring-webapp/src/main/java/org/glassfish/jersey/examples/helloworld/spring/MyApplication.java ) like this: 关键部分是对ResourceConfig进行子类化(例如, https : //github.com/jersey/jersey/blob/master/examples/helloworld-spring-webapp/src/main/java/org/glassfish/ Jersey / ResourceConfig / helloworld / spring /MyApplication.java ),如下所示:

public class MyApplication extends ResourceConfig {

    public MyApplication () {
        register(MyRequestContextFilter.class);
        register(MyResource.class);
        packages("com.mycompany.resources");
        ...
    }
}

You then name your subclass in your app descriptor using javax.ws.rs.Application (eg https://github.com/jersey/jersey/blob/master/examples/helloworld-spring-webapp/src/main/webapp/WEB-INF/web.xml ): 然后,您可以使用javax.ws.rs.Application在应用程序描述符中为子类命名(例如https://github.com/jersey/jersey/blob/master/examples/helloworld-spring-webapp/src/main/webapp/WEB -INF / web.xml ):

<servlet>
    <servlet-name>SpringApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>org.glassfish.jersey.examples.helloworld.spring.MyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

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