Sending nested json object in POST method using Postman to Spring REST API

i am trying to send nested json object in POST request to my spring REST API.

Object java code

public class TestModel {
private String id;
private String name;

public TestModel(String id, String name) {
    this.id = id;
    this.name = name;
}

public String getId() {
    return id;
}

public String getName() {
    return name;
}

}


Post method code in rest controller

@RequestMapping(value = "/helloPost")
public ResponseEntity<TestModel> helloPost(@RequestBody TestModel t) {
    return new ResponseEntity<TestModel>(t, HttpStatus.OK);
}

My postman screenshot

在这里输入图像描述


It has to return status 200 ok and object i sent, but it returns 400 bad request permanently. Please, tell me what am i doing wrong. It was ok when i sent one string(my @RequestBody was string too) but completly not working with custom objects.

PS i have added comma, no changes


As mentioned in the comment, please add the default constructor for TestModel class. It should resolve the problem.

As an additional step, if the web service is going to accept json as input, then add consumes annotation with content type as application json.


You missed the "," after the id field in JSON. proper JSON is your case would be below :-

{
"id" : "1",
"name" : "test"
}

It's a malformed json you are sending to the server. You need to add comma to separate elements in json.

Even postman showing wrong icon at the left.

{
"id" : 1,
"name" : "test"
}

Also you need to add setters and default constructor in object model to set those values.

链接地址: http://www.djcxy.com/p/8530.html

上一篇: 如何使用CURL / CygWin从本地Windows机器发送文件到服务器?

下一篇: 在Post方法中使用Postman发送嵌套的json对象到Spring REST API