在尝试运行客户端程序以将Json传递给服务器时,我在connection.getResponseCode()
方法上收到错误代码为500的错误。我正在使用jersey WebServices来创建此程序。
我查了一下:
web.xml
; 我在这个问题上花了一整天的时间。任何帮助都会非常明显......
我的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);
}
}
}
我的server.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..
任何帮助都会非常明显。
答案 0 :(得分:0)
您的网址在上面的客户端示例中不正确,因此您收到了404响应:
HTTP Status 404 - /Project/v1/bharathi/vishnu
网址格式应为:
<context>/v1/bharathi/vishnu
看起来问题在于上下文。这应该是您在容器中配置的内容(可能是.war文件的名称)。