Spring MVC @RequestParam 对象列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33354534/
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:51:11  来源:igfitidea点击:

Spring MVC @RequestParam a list of objects

springrestspring-mvchttp-posthttprequest

提问by Kingamere

I want to create a page where a person sees a list of users and there are check boxes next to each of them that the person can click to have them deleted.

我想创建一个页面,其中一个人可以看到一个用户列表,并且每个人旁边都有复选框,该人可以单击以将其删除。

In my MVC that consumes a REST API, I want to send a List of Userobjects to the REST API.

在我使用 REST API 的 MVC 中,我想向 REST API 发送一个User对象列表。

Can the @RequestParamannotation support that?

在可@RequestParam注解支持?

For example:

例如:

@RequestMapping(method = RequestMethod.DELETE, value = "/delete")
    public @ResponseBody Integer delete(
            @RequestParam("users") List<Users> list) {
        Integer deleteCount = 0;
        for (User u : list) {
            if (u != null) {
                repo.delete(u);
                ++deleteCount;
            }
        }
        return deleteCount;
    }

In the MVC client, the url would be:

在 MVC 客户端中,url 将是:

List list = new ArrayList<User>();
....
String url = "http://restapi/delete?users=" + list;

回答by Mathias Dpunkt

Request parameters are a Multimap of String to String. You cannot pass a complex object as request param.

请求参数是字符串到字符串的 Multimap。您不能将复杂对象作为请求参数传递。

But if you just pass the username that should work - see how to capture multiple parameters using @RequestParam using spring mvc?

但是,如果您只是传递应该有效的用户名 - 看看如何使用 spring mvc 使用@RequestParam 捕获多个参数?

@RequestParam("users") List<String> list

@RequestParam("users") List<String> list

But I think it would be better to just use the request body to pass information.

但我认为最好只使用请求正文来传递信息。

回答by Warkst

Though this question has been answered and the answer has been accepted, I found that using @RequestBodyinstead of @RequestParamdoes exactly what you want, plus it's very easy.

虽然这个问题已经得到回答并且答案已经被接受,但我发现使用@RequestBody而不是@RequestParam完全符合你的要求,而且它很容易。

You'd end up with @RequestBody List<Integer> list. You can call your web service with an HTTP request (POSTwill definitely work, but DELETEshould work too) with request body: [id1, id2, ...]. If it doesn't work out of the box like that, try adding consumes = MediaType.APPLICATION_JSON_VALUE)to the @RequestMapping.

你最终会得到@RequestBody List<Integer> list. 您可以使用 HTTP 请求(POST肯定会工作,但也DELETE应该工作)使用 request body:调用您的 Web 服务[id1, id2, ...]。如果它不能像那样开箱即用,请尝试添加consumes = MediaType.APPLICATION_JSON_VALUE)@RequestMapping.

回答by Frank.Chang

Spring mvc can support List<Object>, Set<Object>and Map<Object>param, but without @RequestParam.

Spring mvc 可以支持List<Object>,Set<Object>Map<Object>param,但不支持 @RequestParam。

Take List<Object>as example, if your object is User.java, and it like this:

List<Object>为例,如果你的目标是User.java,它是这样的:

public class User {
    private String name;
    private int age;

    // getter and setter
}

And you want pass a param of List<User>, you can use url like this

并且你想传递一个参数List<User>,你可以像这样使用 url

http://127.0.0.1:8080/list?users[0].name=Alice&users[0].age=26&users[1].name=Bob&users[1].age=16

Remember to encode the url, the url after encoded is like this:

记得对url进行编码,编码后的url是这样的:

http://127.0.0.1:8080/list?users%5B0%5D.name=Alice&users%5B0%5D.age=26&users%5B1%5D.name=Bob&users%5B1%5D.age=16

Example of List<Object>, Set<Object>and Map<Object>is displayed in my github.

的示例List<Object>Set<Object>Map<Object>显示在我的 github 中