以编程方式配置Jersey应用程序



我正在尝试使用我自己的应用程序实现或扩展的resourcecconfig或packageresourcecconfig配置我的Jersey应用程序。因此,我的第一次尝试包括将现有的web.xml(实际上我使用web-fragment.xml,因为我的开发库的性质)配置移植到我的应用程序实现。

这是移植

之前的工作web-fragment.xml
<servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>my.pkg.resources;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>my.pkg.Filter1;my.pkg.Filter2</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

下面,修改后的web-fragment.xml

<servlet>
    <servlet-name>my.pkg.MyApplication</servlet-name> <!-- implementation follows -->
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>my.pkg.MyApplication</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

和MyApplication类

// [...]
public class MyApplication extends PackagesResourceConfig {
private static final Logger logger = Logger.getLogger(MyApplication.class);
@Context
ServletConfig config
public MyApplication() {
    super("my.pkg.resources;org.codehaus.jackson.jaxrs");
    super.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    // filters not set
}
@PostConstruct 
public void readInitParams() {
    // read init params from ServletConfig
    // config.getInitParameterNames();
    // ...
}
}

每当我使用第二个版本时,我都会收到以下

mag 27, 2013 12:08:03 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  aero.aice.jerico.rs;org.codehaus.jackson.jaxrs;
mag 27, 2013 12:08:03 PM com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder <init>
INFO: Instantiated the Application class my.package.MyApplication. The following root resource and provider classes are registered: [class com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver, class org.codehaus.jackson.jaxrs.JsonParseExceptionMapper, class org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider, class org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper, class aero.aice.jerico.rs.service.OperationService, class aero.aice.jerico.rs.service.CrudService, class org.codehaus.jackson.jaxrs.JacksonJsonProvider]
mag 27, 2013 12:08:03 PM com.sun.jersey.core.spi.component.ProviderFactory __getComponentProvider
SEVERE: The provider class, class com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver, could not be instantiated. Processing will continue but the class will not be utilized
java.lang.InstantiationException: com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver
mag 27, 2013 12:08:04 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: The class com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver is a not a public class and cannot be instantiated.
  SEVERE: The inner class com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver is not a static inner class and cannot be instantiated.

可以看到,com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver是第一个注册的类,但它不是可实例化的,因为它不是公共的,也不是静态的内部类。我发现关于这个过程的文档很少,特别是如何在初始化期间设置功能和属性。我使用的是Jersey的最后可用版本(1.17.1),但也测试了1.9。是否也可以通过编程方式设置应用程序路径?我在文档中见过@ApplicationPath,但它对我的目的没有用,因为我需要在运行时设置它。我知道这些问题更多,但我认为它们都是一个根源。有人能给我指个正确的方向吗?

确保在扫描资源类的路径上没有jersey lib。

只能在部署时设置的基础URI。使用战争名称或@ApplicationPath。你可以在web.xml中使用servlet-mapping元素覆盖它。

我也遇到过类似的问题:

SEVERE: The class com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver is a not a public class and cannot be instantiated.
SEVERE: The inner class com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver is not a static inner class and cannot be instantiated.

这个错误把我吓坏了…基本上我使用的是GlassFish 3.x。它已经包含了一个jersey.core.jar。但是我需要一个jersey-server库。我的剧本是:

 jersey.core.jar -> MANIFEST.MF -> 1.11.1
 jersey-server-1.17.1

我的问题是我混合了来自不同Jersey版本的jar。当我选择jersey-server-1.11.1时,我不再看到这个错误。

最新更新