【问题标题】:How to write a REST API method for which consume json format for list of objects如何编写一个使用 json 格式的对象列表的 REST API 方法
【发布时间】:2018-05-12 08:24:33
【问题描述】:

我写了一个方法如下。

@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
public boolean createMeasurement(List<MeasurementBean> list ,@HeaderParam(AUTHORIZATION) String authString){
    Gson gson = new Gson();
    LoginSession loginSession=null;
    try{

        loginSession = LoginUtility.validateKey(authString);
        ResultRO<List<HashMap<String, Object>>> resultRO = loginSession
                .execute(new Callable<ResultRO<List<HashMap<String, Object>>>>() {
                    @Override
                    public ResultRO<List<HashMap<String, Object>>> call() throws Exception {
                        String key[] = idKey.split(":");
                        String type = key[0];
                        long testId = Long.parseLong("2");
                        return doGetCreateMeasurement(list ,2);
                    }
                });
        return gson.toJson(true);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    return null;
}

我无法使用 json 格式的对象列表。 我的 json 格式如下

    {
        "beans": [{
        "id": 1133,
        "testConditionGroupId": 0,
        "testId": 0,
        "type": 0,
        "completeFlag": 0,
        "invalidFlag": 0,
        "retentionFlag": 0,
        "delBeforeFlag": 0,
        "dontReplicateFlag": 0,
        "releaseFlag": 0,
        "timeBase": 0.0,
        "name": "MeaResult_TEST_XYZABCD",
        "version": "V.1.0.1",
        "description": "M_Test",
        "mimeType": "MIME",
        "stardDate": "",
        "endDate": "",
        "attributeList": [{
            "id": 0,
            "name": "Test_Attribute1",
            "value": "A",
            "unit": "ms"
        }, {
            "id": 0,
            "name": "Test_Attribute2",
            "value": "B",
            "unit": "ms"
        }],
        "subMatrixList": [{
            "id": 0,
            "name": "Test_SubMatrixBean1_New",
            "version": "V.1.0.0",
            "mimeType": "MimeTyep",
            "NumberOfvalues": 3,
            "noOfRows": 1,
            "localColumnList": [{
                "id": 0,
                "name": "TestName_1_New",
                "version": "V.1.0.0",
                "mimeType": "MimeTyep",
                "sequence_Representation": "explicit",
                "independent": 1,
                "global_Flag": 15,
                "raw_Datatype": 1,
                "dataValues": ["1", "2", "3"],
                "flags": [15, 15, 15],
                "generation_Parameters": [1.0, 2.0, 3.0],
                "externalComponents": [],
                "attributeList": [],
                "meaQuantity": {
                    "id": 0,
                    "name": "TestName_1_New",
                    "unit": "ms",
                    "quantityName": "MeaQuantity_Quantity",
                    "description": "MeaQuantity_Test",
                    "localName": "TestName_1_New",
                    "size": 10,
                    "dataType": 1,
                    "min": 1.0,
                    "max": 10.0,
                    "attributeList": [{
                        "id": 0,
                        "name": "Test_Attribute1",
                        "value": "A",
                        "unit": "ms"
                    }, {
                        "id": 0,
                        "name": "Test_Attribute2",
                        "value": "B",
                        "unit": "ms"
                    }]
                }
            }],
            "attributeList": [{
                "id": 0,
                "name": "Test_Attribute1",
                "value": "A",
                "unit": "ms"
            }, {
                "id": 0,
                "name": "Test_Attribute2",
                "value": "B",
                "unit": "ms"
            }]
        }],
        "meaQuantityList": [{
            "id": 0,
            "name": "TestName_1_New",
            "unit": "ms",
            "quantityName": "MeaQuantity_Quantity",
            "description": "MeaQuantity_Test",
            "localName": "TestName_1_New",
            "size": 10,
            "dataType": 1,
            "min": 1.0,
            "max": 10.0,
            "attributeList": [{
                "id": 0,
                "name": "Test_Attribute1",
                "value": "A",
                "unit": "ms"
            }, {
                "id": 0,
                "name": "Test_Attribute2",
                "value": "B",
                "unit": "ms"
            }]
        }]
    }]
}

但是当我尝试使用邮递员发布请求时,它会出现以下错误

警告:未找到请求类 MeasurementBeans 的消息正文阅读器,ContentType : application/json。

而且方法也没有执行。

【问题讨论】:

    标签: java json rest jax-rs


    【解决方案1】:

    您在 createMeasurement() 方法中接受 List&lt;MeasurementBean&gt; 并且您提到的 JSON 是一个对象。

    {
            "beans": []
     //..
    }
    

    只需删除开头的{、"beans:" 和结尾的},将其作为一个数组,即您只需要传递数组即可。

    【讨论】:

    • 您可能希望查看 json 中的字段及其类型与 MeasurementBean 对象中的字段匹配
    • 我已经从现有的 bean 创建了 json 并且只修改了一些名称以查看它是否工作。但它不工作
    • 根据你使用的框架,你认为需要用@RequestBody 或等效注解来注解List&lt;MeasurementBean&gt; list吗?
    • 你能给我一个如何使用它的链接吗?
    猜你喜欢
    • 2013-06-21
    • 2015-01-08
    • 2020-02-06
    • 2012-01-20
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    相关资源
    最近更新 更多