如何在spring boot api中通过requestbody获取对象列表

时间:2016-02-08 09:35:23

标签: spring spring-mvc spring-boot

通过控制器中的@RequestBody获取对象列表,并处理list中的每个对象以执行业务逻辑。

我试过这个但没有工作

@RequestMapping(value="/updateservicetype", method=RequestMethod.POST,produces="application/json")
    public @ResponseBody ServiceTypesMessage updateServiceType(@RequestBody List<BarberServiceType> serviceTypes,final HttpServletResponse response){

也试过以下:

@RequestMapping(value="/updateservicetype", method=RequestMethod.POST,produces="application/json")
    public @ResponseBody ServiceTypesMessage updateServiceType(@RequestBody BarberServiceType[] serviceTypes,final HttpServletResponse response){

1 个答案:

答案 0 :(得分:5)

以下适用于我

    @RequestMapping(value = "/payments", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Payment> batchCreate(@RequestBody List<Payment> payments) {
    return paymentService.create(payments);
}

你需要杰克逊在课程路径

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.0</version>
</dependency>

Json in put是

[{"sort":"10-20-30","account":"1234"},{"sort":"10-20-30","account":"1234"}]