I'm trying to deserialize a list of Long values:
{
"ids": [1, 2, 3]
}
With the following Method:
@PostMapping(value = "/export")
public ResponseEntity<SomeDto> exportCsv(@RequestBody Long[] ids) {
// Some methods
return ResponseEntity.ok(someDto);
}
But I keep getting the following error: JSON parse error: Cannot deserialize value of type Long
. The Error is the same using List<Long>, just for ArrayList<Long>.
I've tried it with @JsonDeserialize(using = NumberDeserializers.LongDeserializer.class)
, but it seems either it doesn't work or I'm using it wrong.
CodePudding user response:
The request body above represents a POJO class with ids
as a property like below
public class Body {
private Long[] ids;
//getters and setters
}
And then use this POJO as RequestBody
public ResponseEntity<SomeDto> exportCsv(@RequestBody Body body)
CodePudding user response:
With json object you try to send, spring assume it's an object with a member field ids
. If you want to attach directly to a list send the following json as body:
[5, 6, 8]
This will work for:
@PostMapping(value = "/export")
public ResponseEntity<SomeDto> exportCsv(@RequestBody Long[] ids) {
...
}
and
@PostMapping(value = "/export")
public ResponseEntity<SomeDto> exportCsv(@RequestBody List<Long> ids) {
...
}