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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35266289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:58:03  来源:igfitidea点击:

how to get list of objects via requestbody in spring boot api

springspring-mvcspring-boot

提问by Hari

To get list of objects via @RequestBodyin controller and process each object in a listto do a business logic.

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

I have tried this but not working

我试过这个但没有用

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

also tried following:

还尝试了以下内容:

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

回答by Essex Boy

Below works for me

下面为我​​工作

    @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);
}

You will need Hymanson in the class path

您将需要在课程路径中使用Hyman逊

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

Json in put is

输入的 Json 是

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