0

I have a REST api in Spring Boot:

@GetMapping("/test")
public MyClass getData() { 

    return something;
}

This endpoint can be requested with up to 10 RequestParams. I know of course all of the 10 possible RequestParams, however client can chose to request with anywhere between 0 to all 10 of the RequestParams.

Now I need a way to handle this without plugging all 10 RequestParams in as parameters in the getData() method. Is it not possible to register all possible RequestParams in a class, and the use that class as parameter ing the getData()?

Something like this:

public class ParamClass {
    private @RequestParam("ParamOne") String ParamOne;
    private @RequestParam("ParamTwo") Set<String> ParamTwo;
    private @RequestParam("ParamThree") Integer ParamThree;
}

And then

@GetMapping("/test")
public MyClass getData(@RequestParam ParamClass params) { 

    return something;
}

Please note: The parameters can be different types (String, int, Set, etc.) therefore the following solution comes very close but does not solve it because the map requires a consistent value type: how to capture multiple parameters using @RequestParam using spring mvc?

TheStranger
  • 1,387
  • 1
  • 13
  • 35

3 Answers3

1

One way to do that would be receive a Map with the params, the problem is that all the params would have the same type, and you would have to cast to the proper type in your code:

@ResponseBody
public String updateFoos(@RequestParam Map<String,Object> allParams) {
    return "Parameters are " + allParams.entrySet();
}
  • The disadvantage of this is that the 10 known parameters are now un-typed, so I wouldn't recommend this approach. It's how you would do it if the parameters are dynamic, i.e. the names are unknown at compile-time, but for 10 well-known but optional parameters, this is the wrong way to go. – Andreas Jul 07 '21 at 11:08
0

You can define all parameters in your method and set them as 'not required'.

@GetMapping("/test")
public MyClass getData(@RequestParam(name="p1", required=false) String p1,
    @RequestParam(name="p2", required=false) Integer p2,
    ...) { 
    return something;
}

Then you can choose which parameters to define in your url. The others are null. Or you define a default-value in the RequestParam-Annotation.

Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
  • Thank you, I already know that... I am asking: "...I need a way to handle this without plugging all 10 RequestParams in as parameters in the getData() method" – TheStranger Jul 07 '21 at 11:02
  • @Ben You said *"client can chose to request with anywhere between 0 to all 10 of the RequestParams"*, which means you need all 10, so why not use the automatic conversion to declared parameter type you get for free when using `@RequestParam`? Asking not to have to declared all 10 doesn't make any sense. – Andreas Jul 07 '21 at 11:05
0

you can use @RequestBody annotation. It will map the HttpRequest body to class object. As spring boot has jackson in its classpath, it will do automatic deserialization of the inbound HttpRequest body onto a Java object.

public class ParamClass {
 String paramOne;
 String paramTwo;
 ....
 String paramTem;
 
 // default constructor
 
 // all getters
 // all setters

}

@PostMapping("/test")
public MyClass getData(@RequestBody ParamClass params) { 

    return something;
}

Client should send json data to the api /test

Apu Das Gupta
  • 1
  • 1
  • 1
  • 3
  • I was looking for something like this, but it does not work.. It says "Required request body is missing" – TheStranger Jul 08 '21 at 07:56
  • Hi @Ben, please check I have updated the code. Changed @GetMapping("/test") to @PostMapping("/test") So you have to send POST request from you ajax request. For example, if you are using JQuery, you can use, – Apu Das Gupta Jul 20 '21 at 06:56
  • Hi @Ben, have to send POST request from you ajax request. For example, if you are using JQuery, you can use something like `var data ={ "paramOne":"value 1", "paramTwo":"value 2" } $.ajax({ url : 'your-post-url', type : "POST", data : JSON.stringify(data), contentType : 'application/json', dataType : 'json', success : function(result) { //success handler code here } });` you can use simple rest api tool like https://restapi.tools/ – Apu Das Gupta Jul 20 '21 at 07:04