POST请求服务器返回HTTP响应代码:500 / JAVA

时间:2016-11-30 14:17:56

标签: java http-post

我正试图在JAVA中发送POST请求到我本地运行的服务。当我在POSTMAN上发出请求时,如下所示,我得到了200响应以及我正在寻找的JSON数据响应

邮差示例 enter image description here

邮差回复!

[
  {
    "id": "Period10",
    "text": "In 10 years",
    "uniqueId": "946bf224-9031-402e-9753-b665be491acd",
    "extendedResults": {
      "/InsuredAgeMin$value": 0,
      "_order": 0,
      "/InsuredAgeMin/InsuredAgeMax$value": 45,
      "/InsuredAgeMin$id": 0,
      "/InsuredAgeMin/InsuredAgeMax/Term$id": "Period10",
      "name": "/InsuredAgeMin/InsuredAgeMax/Term",
      "/InsuredAgeMin/InsuredAgeMax$id": 45,
      "ref-data-id": "M_282_LSWL.Config.PaymentTerm.2",
      "locale": "en-jp",
      "/InsuredAgeMin/InsuredAgeMax/Term$value": "In 10 years"
    }
  },
  {
    "id": "Period11",
    "text": "In 11 years",
    "uniqueId": "73608932-03b9-4cb6-8cb4-84e01bccb44c",
    "extendedResults": {
      "/InsuredAgeMin$value": 0,
      "_order": 1,
      "/InsuredAgeMin/InsuredAgeMax$value": 45,
      "/InsuredAgeMin$id": 0,
      "/InsuredAgeMin/InsuredAgeMax/Term$id": "Period11",
      "name": "/InsuredAgeMin/InsuredAgeMax/Term",
      "/InsuredAgeMin/InsuredAgeMax$id": 45,
      "ref-data-id": "M_282_LSWL.Config.PaymentTerm.2",
      "locale": "en-jp",
      "/InsuredAgeMin/InsuredAgeMax/Term$value": "In 11 years"
    }
  },

但是当我尝试使用JAVA做同样的事情时,我没有成功。这是我一直在使用的代码。

Java代码

private static JSONObject JSONPostResponse() throws IOException, JSONException {
        String query = "http://localhost:8081/harmony/api/service/referenceData/M_282_LSWL.Config.PaymentTerm.2";
        String json = "{\"ref-data-id\":\"M_282_LSWL.Config.PaymentTerm.2\",\"filter./InsuredAgeMin$id\":{\"value\":30,\"operator\":\"lte\"},\"filter./InsuredAgeMin/InsuredAgeMax$id\":{\"value\":30,\"operator\":\"gte\"},\"name\":{\"value\":\"/InsuredAgeMin/InsuredAgeMax/Term\",\"operator\":\"equal\"},\"locale\":\"en-jp\",\"_sort\":{\"_order\":\"asc\"}}";


        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL url = new URL(query);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("cache-control", "no-cache");
        conn.setRequestProperty("postman-token", "5b70f93d-7358-eeb3-0581-9ec8f46d7b0e");
        conn.setDoOutput(true);
        conn.setDoInput(true);


        OutputStream os = conn.getOutputStream();
        os.write(json.getBytes("UTF-8"));
        os.close();

        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
        JSONObject jsonObject = new JSONObject(result);


        in.close();
        conn.disconnect();

        return jsonObject;
    }

上面的代码给出了以下错误。

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://methmy-qa2-r2.northeurope.cloudapp.azure.com/harmony/api/service/referenceData/LSWL.MainCoverage.PaymentTerm
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1676)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1672)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
    at com.metlife.harmony.selenium.utils.JSONObjectReader.sendPost(JSONObjectReader.java:158)
    at com.metlife.harmony.selenium.utils.JSONObjectReader.main(JSONObjectReader.java:181)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: http://methmy-qa2-r2.northeurope.cloudapp.azure.com/harmony/api/service/referenceData/LSWL.MainCoverage.PaymentTerm
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at com.metlife.harmony.selenium.utils.JSONObjectReader.sendPost(JSONObjectReader.java:153)

我实际上不太清楚我在这里要做什么,我是否认为身体请求错了,我应该创建java pojos吗?

我是否错过了对本地主机的请求?这对于要求发生至关重要吗?

我不太确定,我们将非常感谢任何帮助。

提前谢谢。

亲切的问候,尼科斯。

0 个答案:

没有答案
相关问题