Java HTTP状态代码500是否表示rest错误代码未出现
我正在尝试通过RESTAPI使用HTTP PUT请求更新对象。我得到了不同的错误,比如400个,但现在在修改代码和标题后,我得到了500个,这意味着Java HTTP状态代码500是否表示rest错误代码未出现,java,json,http-response-codes,http-put,Java,Json,Http Response Codes,Http Put,我正在尝试通过RESTAPI使用HTTP PUT请求更新对象。我得到了不同的错误,比如400个,但现在在修改代码和标题后,我得到了500个,这意味着 Internal Error 500 : The server encountered an unexpected condition which prevented it from fulfilling the request. 这是否意味着我不必担心以下错误,即操作已被授权,方法未被禁止等 未经授权的401 付款要求402 禁止40
Internal Error 500 : The server encountered an unexpected
condition which prevented it from fulfilling the request.
这是否意味着我不必担心以下错误,即操作已被授权,方法未被禁止等
- 未经授权的401
- 付款要求402
- 禁止403
- 找不到404
String json = "my json string with escape characters"
String st= "username:password";
byte[] encoded = Base64.encodeBase64(st.getBytes());
String credentials = new String(encoded);
String url= "http://localhost:10108/grc/api/contents/20081";
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept","application/json");
con.setRequestProperty ("Authorization", "Basic " + credentials);
con.setRequestMethod("PUT");
JSONObject jsonObject = (JSONObject)new JSONParser().parse(json);
OutputStream os = con.getOutputStream();
os.write(jsonObject.toJSONString().getBytes());
os.flush();
int responseCode = con.getResponseCode();
我的原始JSON如下
{
"name": "Decommisioned CBU One",
"id": "2116",
"description": "Decommisioned CBU One",
"parentFolderId": "2115",
"fields": {
"field": [{
"dataType": "ID_TYPE",
"id": "29",
"name": "Resource ID",
"value": "2116"
}, {
"dataType": "STRING_TYPE",
"id": "63",
"name": "Comment"
}, {
"dataType": "INTEGER_TYPE",
"id": "60",
"name": "Created By",
"value": 6
}, {
"dataType": "DATE_TYPE",
"id": "59",
"name": "Creation Date",
"value": "2015-02-04T20:23:26.000+05:00"
}, {
"dataType": "STRING_TYPE",
"id": "57",
"name": "Description",
"value": "Decommisioned CBU One"
}, {
"dataType": "DATE_TYPE",
"id": "61",
"name": "Last Modification Date",
"value": "2015-02-04T20:23:26.000+05:00"
}, {
"dataType": "INTEGER_TYPE",
"id": "62",
"name": "Last Modified By",
"value": 6
}, {
"dataType": "STRING_TYPE",
"id": "58",
"name": "Location",
"value": "/_op_sox/Project/Default/BusinessEntity/Emirates NBD PJSC/Emirates NBD Bank/Wholesale Banking/(Decommissioned) Corporate Banking/Decommisioned CBU One/Decommisioned CBU One.txt"
}, {
"dataType": "STRING_TYPE",
"id": "56",
"name": "Name",
"value": "Decommisioned CBU One.txt"
}, {
"dataType": "STRING_TYPE",
"id": "66",
"name": "Orphan"
}, {
"dataType": "STRING_TYPE",
"id": "125",
"name": "OPSS-BusEnt:Business Entity Chart",
"value": "${\"labelKey\" : \"label.hierarchyDiagram.url\", \"path\" : \"/visualization/VizRenderer.jsp\", \"modes\" : [\"view\", \"edit\"],\n \"parameters\" :{\"oid\" : \"$objectId\",\"viz\" : \"EntityHierarchy\"},\n \"popUp\" : { \"windowAttributes\" : \"height=800,width=1000,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,name=_blank\"\n }\n }"
}, {
"dataType": "STRING_TYPE",
"id": "124",
"name": "OPSS-BusEnt:Compliance Owner"
}, {
"dataType": "ENUM_TYPE",
"id": "121",
"name": "OPSS-BusEnt:Entity Type",
"enumValue": {
"id": "229",
"name": "Business",
"localizedLabel": "Business",
"index": 2,
"hidden": false
}
}, {
"dataType": "STRING_TYPE",
"id": "123",
"name": "OPSS-BusEnt:Executive Owner"
}, {
"dataType": "ENUM_TYPE",
"id": "133",
"name": "OPSS-BusEnt:In RCSA Scope",
"enumValue": {
"id": "227",
"name": "No",
"localizedLabel": "No",
"index": 2,
"hidden": false
}
}, {
"dataType": "MULTI_VALUE_ENUM",
"id": "122",
"name": "OPSS-BusEnt:In Scope"
}, {
"dataType": "STRING_TYPE",
"id": "126",
"name": "OPSS-BusEnt:Logo URL"
}, {
"dataType": "CURRENCY_TYPE",
"id": "132",
"name": "OPSS-BusEnt:Risk Appetite",
"baseAmount": 0.0,
"localAmount": 0.0
}]
},
"typeDefinitionId": "6",
"primaryParentId": "2112"
}
在删除linbreaks并使用转义序列之后,剩下的是作为JSON使用的以下字符串
{ \"name\": \"Decommisioned CBU One\", \"id\": \"2116\", \"description\": \"Decommisioned CBU One\", \"parentFolderId\": \"2115\", \"fields\": { \"field\": [{ \"dataType\": \"ID_TYPE\", \"id\": \"29\", \"name\": \"Resource ID\", \"value\": \"2116\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"63\", \"name\": \"Comment\" }, { \"dataType\": \"INTEGER_TYPE\", \"id\": \"60\", \"name\": \"Created By\", \"value\": 6 }, { \"dataType\": \"DATE_TYPE\", \"id\": \"59\", \"name\": \"Creation Date\", \"value\": \"2015-02-04T20:23:26.000+05:00\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"57\", \"name\": \"Description\", \"value\": \"Decommisioned CBU One\" }, { \"dataType\": \"DATE_TYPE\", \"id\": \"61\", \"name\": \"Last Modification Date\", \"value\": \"2015-02-04T20:23:26.000+05:00\" }, { \"dataType\": \"INTEGER_TYPE\", \"id\": \"62\", \"name\": \"Last Modified By\", \"value\": 6 }, { \"dataType\": \"STRING_TYPE\", \"id\": \"58\", \"name\": \"Location\", \"value\": \"/_op_sox/Project/Default/BusinessEntity/Emirates NBD PJSC/Emirates NBD Bank/Wholesale Banking/(Decommissioned) Corporate Banking/Decommisioned CBU One/Decommisioned CBU One.txt\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"56\", \"name\": \"Name\", \"value\": \"Decommisioned CBU One.txt\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"66\", \"name\": \"Orphan\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"125\", \"name\": \"OPSS-BusEnt:Business Entity Chart\", \"value\": \"${\\"labelKey\\" : \\"label.hierarchyDiagram.url\\", \\"path\\" : \\"/visualization/VizRenderer.jsp\\", \\"modes\\" : [\\"view\\", \\"edit\\"],\n \\"parameters\\" :{\\"oid\\" : \\"$objectId\\",\\"viz\\" : \\"EntityHierarchy\\"},\n \\"popUp\\" : { \\"windowAttributes\\" : \\"height=800,width=1000,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,name=_blank\\"\n }\n }\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"124\", \"name\": \"OPSS-BusEnt:Compliance Owner\" }, { \"dataType\": \"ENUM_TYPE\", \"id\": \"121\", \"name\": \"OPSS-BusEnt:Entity Type\", \"enumValue\": { \"id\": \"229\", \"name\": \"Business\", \"localizedLabel\": \"Business\", \"index\": 2, \"hidden\": false } }, { \"dataType\": \"STRING_TYPE\", \"id\": \"123\", \"name\": \"OPSS-BusEnt:Executive Owner\" }, { \"dataType\": \"ENUM_TYPE\", \"id\": \"133\", \"name\": \"OPSS-BusEnt:In RCSA Scope\", \"enumValue\": { \"id\": \"227\", \"name\": \"No\", \"localizedLabel\": \"No\", \"index\": 2, \"hidden\": false } }, { \"dataType\": \"MULTI_VALUE_ENUM\", \"id\": \"122\", \"name\": \"OPSS-BusEnt:In Scope\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"126\", \"name\": \"OPSS-BusEnt:Logo URL\" }, { \"dataType\": \"CURRENCY_TYPE\", \"id\": \"132\", \"name\": \"OPSS-BusEnt:Risk Appetite\", \"baseAmount\": 0.0, \"localAmount\": 0.0 }] }, \"typeDefinitionId\": \"6\", \"primaryParentId\": \"2112\" }
最终,由服务器决定返回什么状态代码,但5xx状态代码(与4xx状态代码相反)的标准含义是服务器端出现了错误(如服务器执行空引用或其他严重错误),然而,4xx状态代码意味着发送的客户端/请求有问题。不幸的是,HTTP status 500实际上只是服务器上意外错误的总括。错误可能在任何地方,甚至在身份验证代码中。因此,有可能在发出401响应之前,电话就中断了 在一个完美的世界中,500个响应不会发生,因为开发人员将捕获所有可能的错误并返回有用的响应
一个服务所有者应该对500个错误感兴趣和关心,而一个客户端不应该导致这些错误(一个确定的客户端可以使用它们来完成服务)。接下来要做的最好的事情是联系服务所有者,让他们在日志中查找错误的原因 “error500”仅仅意味着,无论发生什么错误,您都需要在服务器端对其进行调试。从服务器和/或REST应用程序的错误日志开始。如果您收到500个错误,这意味着服务器接受了您发送的请求,但服务器端发生了一些事情,还有一件事情可能是由于数据无效或数据丢失而发生的