Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何传递列表<&燃气轮机;使用spring-mvc的REST-POST方法_Java_Spring_Rest_Spring Mvc - Fatal编程技术网

Java 如何传递列表<&燃气轮机;使用spring-mvc的REST-POST方法

Java 如何传递列表<&燃气轮机;使用spring-mvc的REST-POST方法,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我试图使用SpringMVC在RESTPOST方法的请求体中传递一个类似值的列表。下面是我的示例代码。请让我知道在requestbody中发送列表的正确方式 @RequestMapping(value = "/userlogin/details", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @Respo

我试图使用SpringMVC在RESTPOST方法的请求体中传递一个类似值的列表。下面是我的示例代码。请让我知道在requestbody中发送列表的正确方式

@RequestMapping(value = "/userlogin/details", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<String> insertLoginDetails(
        @RequestParam("username") String userName,
        @RequestBody List<String> listOfID) {
    return eventAnalyzerHelper.insertUserLoginDetails(userName,
            listOfID);
}
@RequestMapping(value=“/userlogin/details”,method=RequestMethod.POST,products=MediaType.APPLICATION\u JSON\u value,consumes=MediaType.APPLICATION\u JSON\u value)
@应答器
公共响应插入详细信息(
@RequestParam(“用户名”)字符串用户名,
@请求主体列表(listOfID){
返回eventAnalyzerHelper.insertUserLoginDetails(用户名,
listOfID);
}

谢谢

这个例子可能会对您有所帮助

每个
文本输入都有相同的名称:

<form method="post">
  Fruit 1: <input type="text" name="fruits"/><br/>
  Fruit 2: <input type="text" name="fruits"/><br/>
  Fruit 3: <input type="text" name="fruits"/><br/>
  <input type="submit"/>
</form>

基本上,Spring自己处理,如果您有多个具有相同路径/名称的字段,它会自动尝试将其转换为数组或列表。

您需要将列表包装到一个对象中。这样设计更好。如果需要,可以将此对象设置为通用对象,并在其他控制器中多次重复使用。请注意,在调用系统中构造对象时,必须遵循相同的结构

@RequestMapping(value = "/", method = RequestMethod.POST)
public String addFruits(@RequestBody ListWrapper fruits) {
 // ...
}

您不在请求主体中发送任何内容,而是从请求主体接收。你所拥有的有什么问题吗?我通常会制作一个专门的傻瓜类,其中包含一个
列表
字段。这样一来,JSON就会变成这样:
{“foos”:[{“a”:1},{“b”:2},…]}
。即使ankur singhal答案与表单相关,控制器也不关心请求是如何构造的,您仍然可以使用javascript或任何其他脚本语言来构建它。您可以为ListWrapper类共享一个构建水果列表的示例吗?
@RequestMapping(value = "/", method = RequestMethod.POST)
public String addFruits(@RequestBody ListWrapper fruits) {
 // ...
}