Restful WebService call with Jersey client version 2.2

Sami picture Sami · Aug 23, 2013 · Viewed 7k times · Source

CASE

I am trying to fetch User data to Servlet filter using REST service.

POM.xml

<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>

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-------->" +entity);

REST:

@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
    return String.valueOf(super.count());
}

PROBLEM

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)

QUESTION

0) What is wrong in my code?

1) What is the wisest way to fetch data from servlet filter using REST + JPA?

2) If there is another way to do this, better I mean, pls let me know?

3) Is the Jersey Client the only way

4) How can I get EntityManager and call the rest service straight from filter without Jersey Client?

Jersey documentation and examples: http://jersey.java.net/documentation/latest/user-guide.html#d0e2481

Thanks, Sami

Answer

Rockoder picture Rockoder · Jun 4, 2014

0) 500 error code means there is some thing wrong on Server side. May be the action action threw some error even though REST got processed. 1) for simple REST, I have used Restlet and HttpClient. Even jerseyClient with JAXB to process response as POJOs. In my experience, simple HTTP is the best and simplest way to process REST response. You can easily write a wrapper on http code (java.net) to process request/response, create Dom around that 2) and 3) are answered above