Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have method in my REST controller that contains a lot of parameters. For example:

@RequestMapping(value = "/getItem", method = RequestMethod.GET)
public ServiceRequest<List<SomeModel>> getClaimStatuses(
        @RequestParam(value = "param1", required = true) List<String> param1,
        @RequestParam(value = "param2", required = false) String param2,
        @RequestParam(value = "param3", required = false) List<String> param3,
        @RequestParam(value = "param4", required = false) List<String> param4,
        @RequestParam(value = "param5", required = false) List<String> param5) {
    // ......
}

and I would like to map all GET request parameters to a POJO object like:

public class RequestParamsModel {

   public RequestParamsModel() {

   }

   public List<String> param1;
   public String param2;
   public List<String> param3;
   public String param4;
   public String param5;
}

I need something like we can do using @RequestBody in REST Controller.

Is it possible to do in Spring 3.x ?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

Possible and easy, make sure that your bean has proper accessors for the fields. You can add proper validation per property, just make sure that you have the proper jars in place. In terms of code it would be something like

import javax.validation.constraints.NotNull;

public class RequestParamsModel {

    public RequestParamsModel() {}

    private List<String> param1;
    private String param2;
    private List<String> param3;
    private String param4;
    private String param5;

    @NotNull
    public List<String> getParam1() {
        return param1;
    }
    //  ...
}

The controller method would be:

import javax.validation.Valid;

@RequestMapping(value = "/getItem", method = RequestMethod.GET)
public ServiceRequest<List<SomeModel>> getClaimStatuses(@Valid RequestParamsModel model) {
    // ...
}

And the request, something like:

/getItem?param1=list1,list2&param2=ok

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...