Sam*_*ami 5 java rest jpa eclipselink jersey-client
案件
我试图使用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>
Run Code Online (Sandbox Code Playgroud)
码
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);
Run Code Online (Sandbox Code Playgroud)
休息:
@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
return String.valueOf(super.count());
}
Run Code Online (Sandbox Code Playgroud)
问题
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)
Run Code Online (Sandbox Code Playgroud)
题
0)我的代码有什么问题?
1)使用REST + JPA从servlet过滤器获取数据的最明智的方法是什么?
2)如果有另一种方法可以做到这一点,我的意思是更好,请告诉我?
3)泽西岛客户是唯一的方式
4)如何在没有Jersey Client的情况下直接从过滤器获取EntityManager并调用其余服务?
Jersey文档和示例:http: //jersey.java.net/documentation/latest/user-guide.html#d0e2481
谢谢,萨米
0) 500 错误代码表示服务器端出现问题。即使 REST 已得到处理,操作操作也可能会引发一些错误。1)对于简单的REST,我使用了Restlet和HttpClient。甚至 jerseyClient 也使用 JAXB 将响应作为 POJO 进行处理。根据我的经验,简单的 HTTP 是处理 REST 响应的最佳且最简单的方法。您可以轻松地在http代码(java.net)上编写一个包装器来处理请求/响应,围绕上面回答的2)和3)创建Dom
归档时间: |
|
查看次数: |
6804 次 |
最近记录: |