10

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 User objects to the REST API.

Can the @RequestParam annotation support that?

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:

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

4 Answers 4

9

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

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

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh, yes @RequestBody offers the functionality that I am looking for.
@RequestBody is not allowed on a GET for Restful APIs. This will cause problems with openapi libs like springdoc
5

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

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

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

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:

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.

2 Comments

FYI for anyone else confused like me. Using List<User> as parameter for the Controller won't work. Instead a wrapper class containing list should be used a parameter. Check the github link!
I can not get this to work, tried with the same code as in the github link. I am using Spring Boot 2 and Kotlin, so it is probably one of these causing it.
1

Just a reminder, any List of custom objects might require custom converters to be registered, like:

    @Bean
public Converter<String, CustomObject> stringToCustomObjectConverter() {
    return new Converter<>() {
        @Override
        public CustomObject convert(String str) {
            return new ObjectMapper().readValue(str, CustomObject.class);
        }
    };
}

@Bean
public Converter<String, List<CustomObject>> stringToListCustomObjectConverter() {
    return new Converter<>() {
        @Override
        public List<CustomObject> convert(String str) {
            return new ObjectMapper().readValue(str, new TypeReference<>() {
            });
        }
    };
}

So you can cover custom cases like:

 /api/some-api?custom={"name":"Bla 1","age":20}
 /api/some-api?custom={"name":"Bla 1","age":20}&custom={"name":"Bla 2","age":30}
 /api/some-api?custom=[{"name":"Bla 1","age":20},{"name":"Bla 2","age":30}]

where: @RequestParam("custom") List customObjects

Comments

1

In addition to the option with a custom converter, you can make a class that will look like this. (important that the class has a default constructor)

import com.fasterxml.jackson.databind.ObjectMapper;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SortDTO
{

  private String field;
  private String direction;

  public SortDTO(String query) throws JsonProcessingException
  {
    ObjectMapper objectMapper = new ObjectMapper();
    SortDTO sortDTO = objectMapper.readValue(query, SortDTO.class);
    field = sortDTO.getField();
    direction = sortDTO.getDirection();
  }
}

After that you can add your List to the controller parameters like that.

@RequestParam Long someParam1,
@RequestParam List<SortDTO> sort,
@RequestParam String someParam2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.