title: Injection error while trying to get WebServiceContext in RESTful WS with Java Jersey
body: <p>I'm using Jersey for RESTFul Web Services in Java and I'm trying to get the caller IP address within the Java Method, without passing as a parameter.</p>
  
  <p>I tried several other posts with similar questions in StackOverflow but none of them worked. So far I got an error of injection, and I don't know how to solve it.</p>
  
  <p>This piece of code needs to run inside a Tomcat 8 container in one server and JBoss in another server. But when I deploy the project to any of them, the same error is thrown while starting container:</p>
  
  <p><strong><em>SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for field: javax.xml.ws.WebServiceContext com.adamiworks.restfultutorial.JsonService.webServiceContex</em>t</strong></p>
  
  <p>This error also occurs when running from eclipse to Tomcat 8.</p>
  
  <p>Can anyone point me out where I'm wrong?</p>
  
  <p>I have this class:</p>
  
  <pre><code>package com.adamiworks.restfultutorial;
  
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.MediaType;
  import javax.xml.ws.WebServiceContext;
  import javax.xml.ws.handler.MessageContext;
  
  @Path("/json")
  public class JsonService {
  
      @Context
      WebServiceContext webServiceContext;
  
      @GET
      @Path("/{param}")
      @Produces(MediaType.APPLICATION_JSON)
      public JsonObject getMsg(@PathParam("param") String msg) {
  
          HttpServletRequest request = this.getHttpContext();
  
          String clientIpAddress = request.getHeader("X-FORWARDED-FOR");
  
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          Date d = null;
          try {
              d = sdf.parse("1984-01-08");
          } catch (ParseException e) {
              e.printStackTrace();
          }
  
          Cliente c = new Cliente();
          c.setNome("Anyone");
          c.setDataNascimento(d);
  
          JsonObject o = new JsonObject(msg, c);
          o.setText("IP=" + clientIpAddress);
  
          return o;
      }
  
      private HttpServletRequest getHttpContext() {
  
          MessageContext mc = webServiceContext.getMessageContext();
          HttpServletRequest request = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
          return request;
      }
  
  }
  </code></pre>
  
  <p>With this pom.xml</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.adamiworks&lt;/groupId&gt;
      &lt;artifactId&gt;restful-tutorial&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;name&gt;adamiworks restful-tutorial&lt;/name&gt;
      &lt;description&gt;restful-tutorial ftw&lt;/description&gt;
      &lt;properties&gt;
          &lt;jdk.version&gt;1.8&lt;/jdk.version&gt;
          &lt;jersey.version&gt;1.19.3&lt;/jersey.version&gt;
          &lt;javax.ws.rs.version&gt;1.1.1&lt;/javax.ws.rs.version&gt;
      &lt;/properties&gt;
      &lt;build&gt;
          &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.5.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;${jdk.version}&lt;/source&gt;
                      &lt;target&gt;${jdk.version}&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.0&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;warSourceDirectory&gt;webapp&lt;/warSourceDirectory&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet&lt;/artifactId&gt;
              &lt;version&gt;3.1.1&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-bundle&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-core&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&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;${jersey.version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-servlet&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
              &lt;artifactId&gt;jsr311-api&lt;/artifactId&gt;
              &lt;version&gt;${javax.ws.rs.version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>