2013-05-27 68 views
0

我正在嘗試使用我自己的應用程序實現或擴展的ResourceConfig或PackageResourceConfig配置我的Jersey應用程序。所以,我的第一個嘗試就是將一個已存在的web.xml(實際上我使用web-fragment.xml,因爲我的開發的庫特性)配置移植到MyApplication實現中。以編程方式配置Jersey應用程序

這是移植

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

下面前的工作網絡fragment.xml之,修改後的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

此外,我已經看到,使用web.xml中的標準ServletContainer,PackagesResourceConfig會掃描相同的包,但它不會註冊com.sun.jersey.server.impl.application.WebApplicationImpl $ 1WadlContextResolver,因此不會引發異常。 – nex

回答

0

確保您在掃描資源類的路徑上沒有球衣庫。

您只能在部署期間設置的基本URI。使用戰爭名稱或@ApplicationPath。您可以使用web.xml中的servlet-mapping元素覆蓋它。

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時,我再也看不到這個錯誤了。