HTTP状态代码500是否表示未发生其余错误代码

时间:2015-06-27 02:19:45

标签: java json http-response-codes http-put

我正在尝试使用HTTP PUT请求通过REST API更新对象。我得到了不同的错误,如400,但现在修改我的代码和标题后,我得到500,这意味着

 Internal Error 500 : The server encountered an unexpected 
 condition which prevented it from fulfilling the request. 

这是否意味着我不必担心以下错误,这意味着操作被授权,方法不被禁止等等

  • 未经授权的401
  • PaymentRequired 402
  • 禁止403
  • 未找到404

p.s我正在使用JSON。

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\" }

2 个答案:

答案 0 :(得分:2)

不幸的是,HTTP状态500实际上只是服务器上意外错误的全部内容。即使在身份验证代码中,错误也可能在任何地方。因此,即使在发出401响应之前,呼叫也可能爆炸。

在一个完美的世界中,500个响应不会发生,因为开发人员会捕获所有可能的错误并返回有用的响应。

服务所有者应该对500个错误感兴趣并且关注它们,客户端不应该导致它们(确定的客户端可以使用它们来管理服务)。接下来要做的最好的事情是联系服务所有者并要求他们在日志中查找错误原因。

答案 1 :(得分:1)

最终,由服务器决定返回什么状态代码,但5xx状态代码的标准含义(与4xx状态代码相反)是服务器端出现问题(比如服务器执行null dererefence或其他一些严重的错误),而4xx状态代码表示发送的客户端/请求有问题。