我有以下请求 JSON 正文:
在后端我有这个 REST 方法:
@JsonView(RestServiceResponseView.InstitutionUserConnectionAndSchedulerPublic.class)
@RequestMapping(value = "/schedules/{institutionuserconnectionid}", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody List<ScheduleResponseWrapper> createScheduleIntervalContainers(
@PathVariable(value = "institutionuserconnectionid")
final String institutionUserConnectionId,
final @RequestBody(required = true) ScheduleIntervalContainerWrapper scheduleIntervalContainerWrapper) throws BusinessException {
ScheduleIntervalContainerWrapper 看起来像这样:
public class ScheduleIntervalContainerWrapper implements Serializable {
private static final long serialVersionUID = 5430337066683314866L;
private String start;
private String end;
private String startTime;
private String endTime;
public ScheduleIntervalContainerWrapper() {
}
public String getStart() {
return start;
}
public void setStart(final String start) {
this.start = start;
}
public String getEnd() {
return end;
}
静态方法中的 ScheduleIntervalContainerWrapper- 对象不为 null,但字段也不为 null。如果我使用 String 而不是 ScheduleIntervalContainerWrapper- 静态对象服务方法,那么 JSON- String 就可以了 - 因此 JacksonMapper 无法映射字段,但我不知道为什么。 有谁知道我做错了什么? 非常感谢!
最佳答案
Jackson 无法将您的 JSON 映射到该对象,因为您的 JSON 包含在 ScheduleIntervalContainerWrapper
字段(即 scheduleIntervalContainerWrapper
)中找不到的单个元素。
您可以使用 Jackson 来解包 JSON,使用
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
并使用 @JsonRootName(value = "scheduleIntervalContainerWrapper"); 注释您的模型
或者您可以简单地发送不带包装器的 JSON :
{"start" : "13.10.2015", "end" : "13.10.2015", "startTime": "7.0", "endTime" : "19.0"}
关于java - 剩余请求未映射到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088408/