2013-08-23 70 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)在

以上回答