【发布时间】:2015-10-04 21:19:17
【问题描述】:
我正在尝试设置一个可以接受单个对象或相同类型对象列表的端点。
我尝试过用两种数据类型声明两种方法,但 Spring 不喜欢这样(无法启动服务器)
@RequestMapping(
value = "",
method = RequestMethod.POST ,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> single(@RequestBody Something something){
return ResponseEntity.ok("ok");
}
@RequestMapping(
value = "",
method = RequestMethod.POST ,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> multiple(@RequestBody List<Something> somethingList){
return ResponseEntity.ok("ok");
}
我得到的最接近的是接受 Something[] 并声明一个方法。
@RequestMapping(
value = "",
method = RequestMethod.POST ,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> somethingArray(@RequestBody Something... something){
return ResponseEntity.ok("ok");
}
问题:有没有办法做到这一点,而不必只接受一个对象并手动进行反序列化?
编辑:我也尝试了多个@RequestBody,即
@RequestMapping(
value = "",
method = RequestMethod.POST ,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> somethingArray(@RequestBody(required = false) Something something, @RequestBody(required = false) Something[] somethingArray){
return ResponseEntity.ok("ok");
}
【问题讨论】:
标签: java spring rest jackson spring-boot