Java Paypal更新发票rest api HTTP响应代码:500

Java Paypal更新发票rest api HTTP响应代码:500,java,servlets,curl,paypal,Java,Servlets,Curl,Paypal,我目前正在尝试使用Paypal提供的RESTAPI使用servlet创建我自己的服务。我设法使用java将cURL代码传输到HttpsURLConnection中 这是我的密码: JSONObject returnJson = new JSONObject(); PrintWriter out = response.getWriter(); JSONParser jparser = new JSONParser(); try{ String inputStr = request.getP

我目前正在尝试使用Paypal提供的RESTAPI使用servlet创建我自己的服务。我设法使用java将cURL代码传输到HttpsURLConnection中

这是我的密码:

JSONObject returnJson = new JSONObject();
PrintWriter out = response.getWriter();
JSONParser jparser = new JSONParser();
try{
    String inputStr = request.getParameter("input");
    System.out.println(inputStr);
    JSONObject inputJson = (JSONObject) jparser.parse(inputStr);
    String accessToken = (String) inputJson.get("access_token");
    String invoiceId = (String) inputJson.get("invoiceId");

    String url = "https://api.sandbox.paypal.com/v1/invoicing/invoices/"+invoiceId;
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("PUT");
    con.setRequestProperty("Accept-Language", "text/html; charset=UTF-8");
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Authorization", "Bearer "+accessToken);

    //Tentatively, the input is hard coded, after integration, the input comes from http request.

    //However, only merchant email in mandatory for invoice creation in sand box so far
    //For details of invoice attributes please refer to this link--> https://developer.paypal.com/docs/api/#update-an-invoice

    String urlJsonString = "{\"id\":\""+invoiceId+"\",\"status\":\"DRAFT\",\"merchant_info\":{\"email\":\"rui.song.2013-facilitator@sis.smu.edu.sg\",\"first_name\":\"Dennis\",\"last_name\":\"Doctor\",\"business_name\":\"MedicalProfessionals,LLC\",\"phone\":{\"country_code\":\"US\",\"national_number\":\"5032141716\"},\"address\":{\"line1\":\"1234MainSt.\",\"city\":\"Portland\",\"state\":\"LALA\",\"postal_code\":\"97217\",\"country_code\":\"US\"}},\"billing_info\":[{\"email\":\"sally-patient@example.com\"}],\"shipping_info\":{\"first_name\":\"Sally\",\"last_name\":\"Patient\",\"business_name\":\"Notapplicable\",\"address\":{\"line1\":\"1234BroadSt.\",\"city\":\"Portland\",\"state\":\"LALA\",\"postal_code\":\"97216\",\"country_code\":\"US\"}},\"items\":[{\"name\":\"Sutures\",\"quantity\":100,\"unit_price\":{\"currency\":\"USD\",\"value\":\"250\"}}],\"invoice_date\":\"2014-01-07PST\",\"payment_term\":{\"term_type\":\"NO_DUE_DATE\"},\"tax_calculated_after_discount\":false,\"tax_inclusive\":false,\"note\":\"MedicalInvoice16Jul,2013PST\",\"total_amount\":{\"currency\":\"USD\",\"value\":\"250\"}}";

    System.out.println(urlJsonString);
    con.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
    wr.write(urlJsonString);
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);
    out.print(responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer res= new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        res.append(inputLine);
    }
    in.close();
    returnJson = (JSONObject) jparser.parse(res.toString());

    System.out.println(returnJson);
}catch(Exception e){
    e.printStackTrace();
    returnJson.put("message", e);
}
out.print(returnJson);
我正在本地主机上测试服务,我将手动传入两个参数:“access_token”和“invoiceId”,如下所示:

http://localhost:8080/Authentication/PaypalUpdateInvoiceServlet?input={"access_token":"A015Rv3XNo4fmFh4JC2sJiGjl1oEQ5w-B9azU.H6nlzMm1s","invoiceId":"INV2-9TRP-2S2R-OPBD-XK9T"}
这两条信息是我使用上面提到的类似代码获得的。 我只修改了entier HttpsURLConnection部分中的代码,以符合Paypal站点中提供的cURL请求和响应示例。链接-->()

到目前为止,我成功地实现了发票的创建、检索。我使用相同的方法使用所需的特定参数进行servlet调用,并且能够在Paypal站点上获得预期的响应

但现在我被更新发票所困扰。当我调用servlet时

我将收到:

500{"message":java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.sandbox.paypal.com/v1/invoicing/invoices/IINV2-9TRP-2S2R-OPBD-XK9T}

有人能帮我解释一下为什么会出现这个错误,以及我应该如何解决这个问题吗?

这是PayPal api上的一个错误,没有正确处理一些输入,你应该向他们报告。报告已发送,我还有其他方法可以解决这个问题吗?非常感谢@jbarrueta