Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to configure Jersey with Spring using only annotations
    primarykey
    data
    text
    <p>I have a Servlet 3.0 web app that uses both Spring and Jersey. I currently have it set up using the SpringServlet configured as a filter in web.xml, and the resource classes annotated with both <code>@Path</code> and <code>@Component</code>. Here's the web.xml snippet:</p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;jersey-serlvet&lt;/filter-name&gt; &lt;filter-class&gt; com.sun.jersey.spi.spring.container.servlet.SpringServlet &lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt; com.sun.jersey.config.property.packages &lt;/param-name&gt; &lt;param-value&gt;com.foo;com.bar&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.config.feature.FilterForwardOn404&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;jersey-serlvet&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>This setup works, but I really want to get this set up with annotations only - no web.xml config. My first attempt at this was to remove the above SpringServlet configuration and create a class that extends <code>Application</code>. Here's a snippet of that:</p> <pre><code>@ApplicationPath("/*") public class MyApplication extends PackagesResourceConfig { public MyApplication() { super("com.foo;com.bar"); HashMap&lt;String, Object&gt; settings = new HashMap&lt;String, Object&gt;(1); settings.put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, true); this.setPropertiesAndFeatures(settings); } } </code></pre> <p>This works in that the JAX-RS resources are registered and I can hit them at their URLs, but they throw NullPointerExceptions when they try and use their autowired properties... this makes sense because I'm guessing the resources are now being loaded by Jersey and are not Spring managed beans, therefore no autowiring. </p> <p>Despite a fair bit of searching around I cannot find any way of loading the Jersey resources as Spring beans with annotations only. <b>Is there such a way?</b> I don't really want to have to write a bunch of code for the resources to manually fetch the Spring context and invoke the DI if I can help it.</p> <p>If annotations-only isn't going to work, then I can live with the filter config in web.xml if I can specify an <code>Application</code> class to load instead of a list of packages to scan. If I can get rid of the package list in there and just specify an <code>Application</code> class instance then I'll be content.</p> <p>Obviously it would be great if someone had a definitive answer for me but I'd also be grateful for any pointers or hints of where else I could look or things to try.</p> <p>Thanks, Matt</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