Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically configure Jersey Application
    primarykey
    data
    text
    <p>I am trying to configure my Jersey application using my own Application implementation or an extended ResourceConfig or PackageResourceConfig. So, my first attempt consists in porting an exisiting web.xml (actually I'm using web-fragment.xml because of library nature of my development) configuration to MyApplication implementation.</p> <p>This is the working web-fragment.xml before porting</p> <pre><code>&lt;servlet&gt; &lt;servlet-name&gt;rest&lt;/servlet-name&gt; &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt; &lt;param-value&gt;my.pkg.resources;org.codehaus.jackson.jaxrs&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.api.json.POJOMappingFeature&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.spi.container.ContainerRequestFilters&lt;/param-name&gt; &lt;param-value&gt;my.pkg.Filter1;my.pkg.Filter2&lt;/param-value&gt; &lt;/init-param&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;rest&lt;/servlet-name&gt; &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>Below, modified web-fragment.xml</p> <pre><code>&lt;servlet&gt; &lt;servlet-name&gt;my.pkg.MyApplication&lt;/servlet-name&gt; &lt;!-- implementation follows --&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.api.json.POJOMappingFeature&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;my.pkg.MyApplication&lt;/servlet-name&gt; &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>and MyApplication class</p> <pre><code>// [...] 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(); // ... } } </code></pre> <p>Anytime I use the second version, I receive the following</p> <pre><code>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 &lt;init&gt; 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. </code></pre> <p>As you can see, <code>com.sun.jersey.server.impl.application.WebApplicationImpl$1WadlContextResolver</code> is the first registered class but that is not instantiable because it is not public and not a static inner class. I have found very little documentation about this procedure and in particular, how to set features and properties during initialization. I am using the last available version of Jersey (1.17.1) but have tested with 1.9 too. Is it also possible to set application path, programatically? I have seen @ApplicationPath in documentation but it is not useful for my purpose because i need to set it at runtime. I know these are more questions but I think they all lead to the same root. Can anybody point me to the right direction?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload