【问题标题】:Post nested Json to spring controller using Jackson data binding使用 Jackson 数据绑定将嵌套的 Json 发布到 spring 控制器
【发布时间】:2016-03-30 21:22:05
【问题描述】:

我正在尝试在 spring 控制器中检索嵌套的 json 并得到“客户端发送的请求在语法上不正确。”

如果我不使用嵌套的 json 格式,我的代码正在工作并获得正确的数据绑定,因此我可以得出结论,我的 DTO 中可能有问题。

CURL 命令:

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

控制器:

@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;
}
}

映射DTO:

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;
}

}

类型DTO:

public class TypeDTO {
private String type;

public String getType() {
    return type;
}

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

}

【问题讨论】:

    标签: json spring spring-mvc jackson


    【解决方案1】:

    将“private TypeDTO 类型”更改为“private String 类型”后问题解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 2016-07-05
      • 2013-01-11
      • 1970-01-01
      • 2012-10-03
      • 2015-08-02
      相关资源
      最近更新 更多