title: Restful WebService call with Jersey client version 2.2
body: <p><strong>CASE</strong> </p>
  
  <p>I am trying to fetch User data to Servlet filter using REST service.</p>
  
  <p><strong>POM.xml</strong></p>
  
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
          &lt;version&gt;2.2&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
          &lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
          &lt;version&gt;2.0&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
          &lt;version&gt;2.2&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  
      Client client = ClientBuilder.newClient(new ClientConfig());
  
      String entity = client.target("http://localhost:8080/insame/webresources/com.insame.entity.users")
          .path("count")
          .request(MediaType.TEXT_PLAIN)
          .get(String.class);
  
      System.out.println("entity--------&gt;" +entity);
  </code></pre>
  
  <p>REST:</p>
  
  <pre><code>@GET
  @Path("count")
  @Produces("text/plain")
  public String countREST() {
      return String.valueOf(super.count());
  }
  </code></pre>
  
  <p><strong>PROBLEM</strong></p>
  
  <pre><code>javax.ws.rs.ProcessingException: java.net.SocketException: Unexpected end of file from server
  at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:202)
  at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215)
  at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
  
  WARNING:   StandardWrapperValve[com.insame.service.ApplicationConfig]: Servlet.service() for servlet com.insame.service.ApplicationConfig threw exception
  javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
      at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:904)
      at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:749)
      at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88)
      at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
  </code></pre>
  
  <p><strong>QUESTION</strong></p>
  
  <p>0) What is wrong in my code?</p>
  
  <p>1) What is the wisest way to fetch data from servlet filter using
              REST + JPA?</p>
  
  <p>2) If there is another way to do this, better I mean,
          pls let me know?</p>
  
  <p>3) Is the Jersey Client the only way</p>
  
  <p>4) How can I
          get EntityManager and call the rest service straight from filter
          without Jersey Client?</p>
  
  <p>Jersey documentation and examples:
  <a href="http://jersey.java.net/documentation/latest/user-guide.html#d0e2481">http://jersey.java.net/documentation/latest/user-guide.html#d0e2481</a></p>
  
  <p>Thanks,
  Sami</p>