从 Java 调用 Web 服务

发布于 10-20 13:41 字数 752 浏览 6 评论 0原文

我正在尝试开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清泪尽2024-10-27 13:41:52

你能提供整个异常吗?如果没有这个,就很难知道它是否是身份验证问题、编组问题等。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

青朷2024-10-27 13:41:52

如果 JAXB 无法解组您的 XML,则您的 XML 无效(即不是真正的 XML),或者它不符合服务器期望的模式。答案位于异常的堆栈跟踪中的某个位置,其中应该提到解组器不喜欢您的 XML 的原因。

If JAXB is unable to unmarshal your XML, then either your XML is invalid (i.e. is not really XML), or it doesn't conform with the schema expected by the server. The answer is somewhere in the stack trace of the exception, which should mention why the unmarshaller didn't like your XML.

凉城凉梦凉人心2024-10-27 13:41:52

不确定如何进行 POST,但如果使用 Apache HttpClient,您可能会发现事情变得更简单 库。

Not sure how you're making the POST, but you might find things simpler if you use the Apache HttpClient library.

将军与妓2024-10-27 13:41:52

对于“从 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"}

For "Calling a webservice from Java" we can issue simple GET or POST requests.

Below is the code for POST (as requirement is for 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");
    }
}

I have used "https://jsonplaceholder.typicode.com" to achieve this POST Request. Through this website we can perform both GET and POST and test our webservices.

Output will be :

    {
      "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"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文