0

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

这是移植前的工作 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,但它对我的目的没有用,因为我需要在运行时设置它。我知道这些问题更多,但我认为它们都指向同一个根源。谁能指出我正确的方向?

4

2 回答 2

0

我经历过类似的问题:

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.core.jar -> MANIFEST.MF -> 1.11.1
 jersey-server-1.17.1

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

于 2013-08-08T13:40:02.387 回答
0

确保在扫描资源类的路径上没有球衣库。

只能在部署期间设置的基本 URI。使用战争名称或@ApplicationPath. 您可以使用servlet-mappingweb.xml 中的元素覆盖它。

于 2013-07-25T21:02:03.780 回答