java - 如何编写使用 json 格式作为对象列表的 REST API 方法

标签 java json rest jax-rs

我写了一个方法如下。

@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"
            }]
        }]
    }]
}

但是当我尝试使用 postman POST 请求时,出现以下错误

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

而且方法也没有执行。

最佳答案

您正在接受 List<MeasurementBean>createMeasurement()方法和你提到的 JSON 是一个对象。

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

只需删除开头的 { 即可将其作为数组, "beans:"并结束} ,即您只需要传递数组。

关于java - 如何编写使用 json 格式作为对象列表的 REST API 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304240/

相关文章:

java - Web 应用程序、离线模式和同步

java - 字符串org.json.JSONException : Unterminated object at character

javascript - C# 数组到 JavaScript 数组

javascript - 即使正确设置了 document.domain,跨子域 ajax 请求也被拒绝

json - 如何有效地同时支持 JSR-303 验证和 Jackson JSON 映射?

java - 连接 Java 和 Python Flask

Java POI 提供的数据似乎在 Office 2007+ XML 中

java - 什么是java新的?函数或关键字

linux - 适用于 Linux 上 REST 和/或 SOAP Web 服务开发的堆栈/框架

wcf - 用于测试 WCF Rest 服务的测试客户端还是仅使用浏览器?