title: Jersey REST service as Spring Component
body: <p>I hava a rest service inside my app and i would like it to use spring beans.
  Spring i am using is version 2.5.
  Part of web.xml:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;jersey-servlet&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;com.app.rest&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;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>In pom.xml i have dependencies:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
      &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
      &lt;version&gt;1.8&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
          &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-json&lt;/artifactId&gt;
          &lt;version&gt;1.8&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>In context.xml i have added:</p>
  
  <pre><code>&lt;context:component-scan base-package="com.app.rest"/&gt;
  </code></pre>
  
  <p>and also:</p>
  
  <pre><code>&lt;tx:annotation-driven proxy-target-class="true" /&gt;
  </code></pre>
  
  <p>Interesting part of service class:</p>
  
  <pre><code>@Component
  @Path("/notification")
  @Scope("singleton")
  public class PushNotification {
  
      @Autowired
      //@Resource(name = "sendMessageServiceFacade")
      private SendMessageServiceFacade sendMessageServiceFacade;
  
      @Autowired
      //@Resource(name = "responseCode")
      private NotificationResponseMessage responseCode;
  
      @Autowired
      //@Resource(name = "validator")
      private PushMessagePreValidator validator;
  
      @POST
      @Path("/register")
      @Consumes(MediaType.APPLICATION_JSON)
      public Response registerDevice(PushWooshRegisterDeviceDto dto) {
          if (validator.validate(dto) == 200) {
              return invokePushMessage(dto, PushMessageType.REGISTER.getMsgType());
          } else {
              return buildFailResponse();
          }
      }
  
      @GET
      @Path("/{param}")
      public Response ping(@PathParam("param") String param) {
          return Response.status(200).entity(param).build();
      }
  </code></pre>
  
  <p>GET method works as expected (basicly it works :) ), but POST method throws NullPointerException on line: if (validator.validate(dto) == 200).</p>
  
  <p>I dont know why @Autowired annotation doesnt work here, ale i tried with @Resource - same effect.</p>
  
  <p>Any tip may help.</p>
  
  <p>Thanks in advance,
  Marek.</p>