【问题标题】:Webservice returns a Server error 500Webservice 返回服务器错误 500
【发布时间】:2015-04-30 15:57:04
【问题描述】:

在尝试运行客户端程序以将 Json 传递给服务器时,我在 connection.getResponseCode() 方法上收到错误代码 500。我正在使用 jersey WebServices 创建此程序。

我检查了我的:

  1. 类名;
  2. 包名;
  3. web.xml;
  4. 我的服务器是否处于启动状态(使用 apache tomcat 6)。

我整天都在处理这个问题。任何帮助都将非常感激....

我的web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Projectname</display-name>
    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.projectname.controller</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/v1/*</url-pattern>
    </servlet-mapping>

client.java

public class Client {

    public static void main(String[] args) {
        BufferedReader in=null;
        try {
            String JSON_DATA =
                 "{" 
               + "  \"ProjectRequest\": [" 
               + "    {" 
               + "      \"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/Projectname/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 response:::"+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);
            // System.out.println(m[0]);
            // System.out.println(m[1]);


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

我的服务器.java

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

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

我从控制台收到的错误消息

declared string in json format{  "ProjectRequest": [    {      "AuthenticationType": "email",      "EmailAddress": "test@gmail.com",      "Password" : "12345",      "PracticeID" : "null",      "DeviceID" : "null"    }   ]}
{"ProjectRequest":[{"PracticeID":"null","DeviceID":"null","AuthenticationType":"email","Password":"12345","EmailAddress":"test@gmail.com"}]}
connection completed
connection response:::404
error
error stream....sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1f297e7
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 404 - /Project/v1/bharathi/vishnu</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/Project/v1/bharathi/vishnu</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.43</h3></body></html>

REST Service Invoked Successfully..

任何帮助都将不胜感激。

【问题讨论】:

  • “我在 connection.getResponseCode() 方法上收到错误代码 500” - 显然不是。您引用的输出说明了其他内容。
  • 请求的资源不可用。 这是404,而不是500。您发布的内容与您的问题有何对应?

标签: java web-services maven jersey


【解决方案1】:

您在上面的客户端示例中的 URL 不正确,因此您会收到 404 响应:

HTTP Status 404 - /Project/v1/bharathi/vishnu

网址应采用以下格式:

<context>/v1/bharathi/vishnu

看起来问题在于上下文。这应该是您在容器中配置的内容(可能是 .war 文件的名称)。

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2013-07-24
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多