2013-08-23 65 views
5

CASERESTful Web服务调用使用Jersey客户端2.2版

我试图获取用户数据使用REST服务Servlet过滤器。

的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()); 
} 

问题

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) 

问题

0)什么是错在我的代码?

1)使用 REST + JPA从servlet过滤器获取数据的最明智的方法是什么?

2)如果还有其他方法可以做到这一点,我的意思是, 请告诉我吗?

3)为Jersey客户端的唯一途径

4)我怎样才能得到 的EntityManager并直接从过滤器 没有Jersey客户端调用REST服务?

新泽西文档和示例: http://jersey.java.net/documentation/latest/user-guide.html#d0e2481

感谢, 萨米

回答

0

0)500错误代码的含义有一些东西错了服务器端。即使REST得到处理,也可能是行为操作抛出了一些错误。 1)对于简单的REST,我使用了Restlet和HttpClient。即使jerseyclient与JAXB处理响应作为POJO。根据我的经验,简单的HTTP是处理REST响应的最好和最简单的方法。您可以轻松地在http代码(java.net)上编写一个包装来处理请求/响应,创建Dom,并且3)在

以上回答