简体   繁体   English

如何在 Spring Boot 中反序列化数组对象

[英]How to deserialize an array object in spring boot

my question is very simple, I have a spring boot controller that recibes a array of objects like that:我的问题很简单,我有一个 spring boot 控制器,它可以接收这样的对象数组:

@RequestMapping(value = "/contracts/{id}/milestones", method = RequestMethod.PUT, headers = "Accept=application/json")
    @ResponseBody
    @Transactional
    public ResponseEntity<?> updateContractMilestones(@PathVariable("id") String contractId,
            @DTO(MilestoneUpdateStateDto.class) List<Milestone> milestones,UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request) {
}

But when I send the following Json object:但是当我发送以下 Json 对象时:

[{"id":18,"state":"FINALIZED"},{"id":19,"state":"FINALIZED"}]

I get the following error:我收到以下错误:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of com.espiritware.opusclick.dto.MilestoneUpdateStateDto out of START_ARRAY token;无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_ARRAY 令牌中反序列化com.espiritware.opusclick.dto.MilestoneUpdateStateDto实例; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com.espiritware.opusclick.dto.MilestoneUpdateStateDto out of START_ARRAY token嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌中反序列化com.espiritware.opusclick.dto.MilestoneUpdateStateDto实例

My question is what should I do from the backend side to be able to receive this object without errors?我的问题是我应该从后端做什么才能无错误地接收这个对象?

Many Thanks!非常感谢!

update your method signature to将您的方法签名更新为

@RequestMapping(value = "/contracts/{id}/milestones", method = RequestMethod.PUT, headers = "Accept=application/json")
@ResponseBody
@Transactional
public ResponseEntity<?> updateContractMilestones(
    @PathVariable("id") String contractId, 
    List<MilestoneUpdateStateDto> milestones,
    ...
)

and try with this JSON并尝试使用此 JSON

{
 [
  {"id":18, "state":"FINALIZED"},
  {"id":19, "state":"FINALIZED"}
 ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM