使用Jackson数据绑定将嵌套的Json发布到spring控制器

时间:2022-09-22 20:37:42

Well I am trying to retrieve a nested json in spring controller and getting "The request sent by the client was syntactically incorrect."

好吧,我试图在spring控制器中检索一个嵌套的json并获得“客户端发送的请求在语法上不正确”。

My code is working and getting the correct data binding if I don't do nested json format, so I can conclude that maybe something is not right in my DTO.

如果我不做嵌套的json格式,我的代码正在工作并获得正确的数据绑定,所以我可以得出结论,在我的DTO中可能有些不对。

CURL Command:

CURL -i -H "Content-Type: application/json" -X POST http://localhost:8080/insertMapping -d '{"mapping": {"adobe_segment_id": "125", "dp_key_id": "1"}, "type": "adobe"}'

JSON:

{"mapping": {"adobe_segment_id": "125", "dp_key_id": "1"}, "type": "adobe"}

Controller:

@RequestMapping(value = "/insertMapping", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> createUser(@RequestBody RequestBodyDTO mapping) {
    LOG.info("/insertMapping" + "   ,type:" + mapping.getType().getType());
    return null;
}

RequestBodyDTO:

public class RequestBodyDTO {
private MappingDTO mapping;
private TypeDTO type;

public TypeDTO getType() {
    return type;
}

public void setType(TypeDTO type) {
    this.type = type;
}

public MappingDTO getMapping() {
    return mapping;
}

public void setMapping(MappingDTO mapping) {
    this.mapping = mapping;
}
}

MappingDTO:

public class MappingDTO {
// adobe
private Integer adobe_segment_id;
private Integer dp_key_id;


public Integer getAdobe_segment_id() {
    return adobe_segment_id;
}

public void setAdobe_segment_id(Integer adobe_segment_id) {
    this.adobe_segment_id = adobe_segment_id;
}

public Integer getDp_key_id() {
    return dp_key_id;
}

public void setDp_key_id(Integer dp_key_id) {
    this.dp_key_id = dp_key_id;
}

}

TypeDTO:

public class TypeDTO {
private String type;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

}

1 个解决方案

#1


0  

Problem resolved after I changed "private TypeDTO type" to "private String type".

将“private TypeDTO type”更改为“private String type”后问题解决了。

#1


0  

Problem resolved after I changed "private TypeDTO type" to "private String type".

将“private TypeDTO type”更改为“private String type”后问题解决了。