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

问题描述 投票:5回答:1

案件

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

POM.hml

<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>

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);

休息:

@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客户端直接致电给其余的服务?

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

谢谢,萨米

java rest jpa eclipselink jersey-client
1个回答
0
投票

0)500错误代码的含义有一些东西错了服务器端。可能是行动对行动扔即使REST得到处理一些错误。 1)为简单的REST中,我使用的Restlet和HttpClient的。即使jerseyClient与JAXB处理响应视为POJO。根据我的经验,简单的HTTP是处理REST响应的最好和最简单的方法。你可以很容易写在http代码(java.net)的包装处理的请求/响应,即2的周围创建DOM的)和3)以上回答

© www.soinside.com 2019 - 2024. All rights reserved.