简体   繁体   中英

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:

@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:

[{"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; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com.espiritware.opusclick.dto.MilestoneUpdateStateDto out of START_ARRAY token

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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