使用jersey进行webservices时没有得到正确的响应

时间:2015-04-30 09:55:34

标签: java web-services rest servlets jersey

我正在使用jersey(而不是maven)开发web服务。我收到错误,如下面的堆栈跟踪(错误500)所示。我安装了apache tomcat6作为本地服务器。

  declared string in json format{  "SnapshotRequest": [    {      "AuthenticationType": "email",      "EmailAddress": "test@gmail.com",      "Password" : "12345",      "PracticeID" : "null",      "DeviceID" : "null"    }   ]}
    {"SnapshotRequest":[{"PracticeID":"null","DeviceID":"null","AuthenticationType":"email","Password":"12345","EmailAddress":"test@gmail.com"}]}
    connection completed
    500
    error
    error stream....sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@939ec3
    close connection
    read from server<html><head><title>Apache Tomcat/6.0.43 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Servlet.init() for servlet jersey-servlet threw exception</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>Servlet.init() for servlet jersey-servlet threw exception</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>javax.servlet.ServletException: Servlet.init() for servlet jersey-servlet threw exception

    REST Service Invoked Successfully..

我已经创建了一个客户端程序(如指定http://crunchify.com/create-very-simple-jersey-rest-service-and-send-json-data-from-java-client/),如下所示.Below是myclient程序......

public class Client {
    //"http://localhost:8080/Snapshothealthapp1/rest/json/product/get"
    public static void main(String[] args) {
        BufferedReader in=null;
        try {
        String JSON_DATA =
                 "{" 
               + "  \"SnapshotRequest\": [" 
               + "    {" 
               + "      \"AuthenticationType\": \"email\"," 
               + "      \"EmailAddress\": \"test@gmail.com\","                  
               + "      \"Password\" : \"12345\"," 
               + "      \"PracticeID\" : \"null\"," 
               + "      \"DeviceID\" : \"null\""
               + "    }   ]"
           + "}";


        System.out.println("declared string in json format" + JSON_DATA);

             JSONObject jsonObject = new JSONObject(JSON_DATA);
                System.out.println(jsonObject);

            URL url = new URL("http://localhost:8080/Snapshothealthapp1/v1/bharathi/vishnu");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();


            connection.setRequestProperty("Content-Type","application/json; charset:ISO-8859-1;");
            connection.setDoOutput(true);
            connection.connect();  
            System.out.println("connection completed");
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
            out.write(jsonObject.toString());

        System.out.println(""+connection.getResponseCode());
          if(connection.getResponseCode() == 200)
          {
            System.out.println("success");
            in = new BufferedReader(new
                    InputStreamReader(connection.getInputStream()));
          }
          else
          {
            System.out.println("error");
            in = new BufferedReader(new
                    InputStreamReader(connection.getErrorStream()));
             System.out.println("error stream...."+connection.getErrorStream());
          }
            out.close();
            System.out.println("close connection");

            String s= in.readLine();
            int[] m= new int[2];


            System.out.println("read from server" +s);


            while (in.readLine() != null) {
            }
            System.out.println("\nREST Service Invoked Successfully..");
            in.close();
        } catch (Exception e) {
            System.out.println("\nError while calling REST Service");
            System.out.println(e);
        }
}
}

我的服务器看起来像

@Path("/bharathi")
public class Test {
    @POST
    @Path("/vishnu")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response crunchifyREST(InputStream incomingData) {
        StringBuilder crunchifyBuilder = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
            String line = null;
            while ((line = in.readLine()) != null) {
                crunchifyBuilder.append(line);
            }
        } catch (Exception e) {
            System.out.println("Error Parsing: - ");
        }
        System.out.println("Data Received: " + crunchifyBuilder.toString());

        // return HTTP response 200 in case of success
        return Response.status(200).entity(crunchifyBuilder.toString()).build();
    }
}

我坐在这前面最后4个小时。我无法弄清楚这里发生了什么。我无法从那里得到答案......我得到的错误没有500 ..

在我的浏览器(chrome)上测试了网址后,我得到了以下内容......

HTTP Status 500 - Servlet.init() for servlet jersey-servlet threw exception

type Exception report

message Servlet.init() for servlet jersey-servlet threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: Servlet.init() for servlet jersey-servlet threw exception
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
        org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        java.lang.Thread.run(Unknown Source)
    root cause

    com.sun.jersey.spi.inject.Errors$ErrorMessagesException
        com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
        com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:137)
        com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:203)
        com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:695)
        com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:690)
        com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:438)
        com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:287)
        com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:587)
        com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:213)
        com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:342)
        com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:516)
        javax.servlet.GenericServlet.init(GenericServlet.java:212)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
        org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        java.lang.Thread.run(Unknown Source)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.43 logs.

    Apache Tomcat/6.0.43 

这是否与我的本地服务器有关?任何帮助都会非常明显......提前谢谢.....

0 个答案:

没有答案