从 Java 调用 Web 服务
我正在尝试开发一个 Java 程序,该程序将简单地调用目标 URL 上的 Web 服务。
请求方式为POST,(不支持GET)。
为此,在我的程序中,我使用 java.net.* 库。我想在post请求中发送一个xml文件。每当我运行客户端程序时,它都会出现以下错误:
java.io.IOException:server returned response code 500
然后,当我检查服务器日志时,出现以下异常:
org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/targetdirectory] threw exception [Request processing failed; nested exception is org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException - with linked exception:.....
在服务器端,我使用 jaxb2marshaller,框架是 spring 3.0 mvc。
所有其他客户端(例如 php 中的客户端)都能够使用 php cURL 调用相同的 Web 服务。
I am trying to develop a Java program that will simply call a webservice on a target URL.
Request method is POST, (GET not supported).
For this in my program I am using java.net.*
library. I want to send an xml file in the post request. Whenever i run the client program it gives me following error:
java.io.IOException:server returned response code 500
Then when I check in the server logs there is following exception:
org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/targetdirectory] threw exception [Request processing failed; nested exception is org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException - with linked exception:.....
On the server side I am using jaxb2marshaller, framework is spring 3.0 mvc.
All the other clients such as in php are able to invoke the same webservice using php cURL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
对于“从 Java 调用 Web 服务”,我们可以发出简单的 GET 或 POST 请求。
下面是 POST 的代码(因为帖子的要求):
public static void MyPOSTRequest() throws IOException {
final String POST_PARAMS = "{\n" + "\"userId\": 101,\r\n" + " \"id\": 101,\r\n"+ " \"title\": \"Test Title\",\r\n" + " \"body\": \"Test Body\"" + "\n}";
System.out.println(POST_PARAMS);
URL obj = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
postConnection.setRequestMethod("POST");
postConnection.setRequestProperty("userId", "a1bcdefgh");
postConnection.setRequestProperty("Content-Type", "application/json");
postConnection.setDoOutput(true);
OutputStream outputStream = postConnection.getOutputStream();
outputStream.write(POST_PARAMS.getBytes());
outputStream.flush();
outputStream.close();
int responseCode = postConnection.getResponseCode();
System.out.println("POST Response Code : " + responseCode);
System.out.println("POST Response Message : " +
postConnection.getResponseMessage());
if (responseCode == HttpURLConnection.HTTP_CREATED) {
BufferedReader in = new BufferedReader(new InputStreamReader(postConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString()); // print result
} else {
System.out.println("POST NOT WORKED");
}
}
我使用了“https://jsonplaceholder.typicode.com< /a>”来实现这个POST请求。通过这个网站,我们可以执行 GET 和 POST 并测试我们的网络服务。
输出将是:
{
"userId": 101,
"id": 101,
"title": "Test Title",
"body": "Test Body"
}
POST Response Code : 201
POST Response Message : Created
{ "userId": 101, "id": 101, "title": "Test Title", "body": "Test Body"}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
你能提供整个异常吗?如果没有这个,就很难知道它是否是身份验证问题、编组问题等。HttpClient
def 可以工作,也可以查看 Play 的 WS 库:
http://www.playframework.org/documentation/api /1.1.1/play%2Flibs%2FWS.html
Can you provide the whole exception? Without that, it's hard to know if it's an authentication problem, a marshalling problem, etc.
HttpClient def works, checkout Play's WS library as well:
http://www.playframework.org/documentation/api/1.1.1/play%2Flibs%2FWS.html