我正在尝试使用自己的Application实现或扩展的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,但它对我的目的没用,因为我需要在运行时设置它。
我知道这些问题更多,但我认为它们都是相同的根源。
有人能指出我正确的方向吗?
答案 0 :(得分:0)
确保在路径上没有针对资源类扫描的jersey libs。
您只能在部署期间设置的基本URI。使用战争名称或@ApplicationPath
。您可以使用web.xml中的servlet-mapping
元素覆盖它。
答案 1 :(得分: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
我的问题是我混合了不同泽西版本的JAR。当我去jersey-server-1.11.1时,我不再看到这个错误了。