在Java中部署REST客户端时出错

时间:2015-01-29 04:45:36

标签: java eclipse rest tomcat

我正在使用eclipse luna和jdk 7.我正在使用tomcat服务器。我通过创建动态Web项目来编写简单的休息程序,它的工作正常。我已经创建了独立的java程序来测试休息程序我正在获得以下异常。

我的REST计划

 @Path("/helloworld")
    public class test {
    // When client wants XML
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHelloXML() { 
    return "<?xml version=\"1.0\"?>" + "<msg>" + "Hello World in REST" + " 
    </msg>";
    }
    Web. xml
    <servlet-mapping>
    <servlet-name>spring-ws-service</servlet-name>
    <url-pattern>/processXML</url-pattern>
    </servlet-mapping>
    <servlet>
    <servlet-name>Rest</servlet-name>
    <servlet-class>
    com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Rest</servlet-name>
    <url-pattern>/backend/*</url-pattern>
    </servlet-mapping>

当我查看网址https://lcalhoast:8443/Phoenix/backend/helloworld时 我将输出为Hello World in Rest。

我创建了独立的java程序的Rest客户端。 这是程序

 public class Test {
    public static void main(String[] args) {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());
    System.out.println(service.path("backend").path("helloworld").accept(
    MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());    
    System.out.println(service.path("backend").path("helloworld").accept(
    MediaType.TEXT_XML).get(String.class));
    System.out.println(service.path("backend").path("helloworld").accept(
    MediaType.TEXT_HTML).get(String.class));
    }
    private static URI getBaseURI() {
    return UriBuilder.fromUri(
    "http://localhost:8443/Phoenix").build();
    }
    }
When i run client as java application. i am getting following ecxceptions

`线程中的异常&#34; main&#34; com.sun.jersey.api.client.ClientHandlerException:

java.net.SocketException: Unexpected end of file from server
com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509)
at cs9322.simple.rest.hello.client.Test.main(Test.java:22)
Caused by: java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:772)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:769)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)

我重启服务器仍然在两天后收到同样的错误

0 个答案:

没有答案