[英]Spring Boot PostMapping deserialize Long array
I'm trying to deserialize a list of Long values:我正在尝试反序列化 Long 值列表:
{
"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
.但我不断收到以下错误:
JSON parse error: Cannot deserialize value of type Long
。 The Error is the same using List<Long>, just for ArrayList<Long>.错误与使用 List<Long> 相同,仅适用于 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.我已经用
@JsonDeserialize(using = NumberDeserializers.LongDeserializer.class)
试过了,但它似乎要么不起作用,要么我用错了。
The request body above represents a POJO class with ids
as a property like below上面的请求体表示一个 POJO 类,其中
ids
作为属性,如下所示
public class Body {
private Long[] ids;
//getters and setters
}
And then use this POJO as RequestBody然后使用这个 POJO 作为 RequestBody
public ResponseEntity<SomeDto> exportCsv(@RequestBody Body body)
With json object you try to send, spring assume it's an object with a member field ids
.对于您尝试发送的 json 对象,spring 假定它是一个具有成员字段
ids
的对象。 If you want to attach directly to a list send the following json as body:如果您想直接附加到列表,请将以下 json 作为正文发送:
[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) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.