2

Is there a "spring way" to parse deepObjects given in url as parameters? Let's say I have a "Filter" object with JSON representation:

{
 "id": 5,
 "name": "Andrew"
}

Documentation of project says, that this is "deepObject", so when I receive url with this object as a parameter, i should expect:

hostname/api/person?filter[id]=5&filter[name]=Andrew

My problem is that I cannot parse this parameter into object using, known to me, spring ways. After creation Object "Field" with the same fields as JSON. I tried using annotation @RequestParam

@RequestMapping(value = "api/person", method = RequestMethod.GET)
public ResponseEntity<List<Person>> getPerson(
          @RequestParam Filter filter
) {
    //method body..
}

and also not using this annotation - as it suppose to be, according to documentation...

@RequestMapping(value = "api/person", method = RequestMethod.GET)
public ResponseEntity<List<Person>> getPerson(
       Filter filter
) {
        //method body..
}

In both ways i ended up with having "null" as a result. Even, when trying to parse this parameters to generic "Object", null is only thing that comes out of it.

Now i am using MultiValueMap<String, String> allParams, as one of @RequestParam, and parse parameters manually to desired form, but it is not seems to be the best solution.

Is there a better (correct) way to do that? I am using Java 1.6 and spring 4.0.9 (limited by jdk).

Thank you in advance!

@Edit According to comments, it looks like there is no spring mechanism to do that.

Bebido
  • 21
  • 2
  • Instead of doing it `hostname/api/person?filter[id]=5&filter[name]=Andrew`, you can also use it like `hostname/api/person?filter=true&id=5&name=Andrew`. You can maintain a map based upon the filter is enabled and then you can convert into an object. – Sambit Feb 02 '21 at 16:56
  • Thank you for answer. Unfortunately, i am not able to change shape of incoming request ;( – Bebido Feb 02 '21 at 17:02
  • If you are not changing the request style, you will have to do this manually. `Spring MVC` **doesn't have default conversion strategy** from multiple parameters with the same name to Map, List etc. – İsmail Y. Feb 02 '21 at 19:26
  • It's a pity. Thank you for your time :) – Bebido Feb 03 '21 at 08:17
  • I was running into the same issue. I am coming from an generated openapi client. while swagger-ui does form serialization, which Spring web is able to handle, the deepObject is not resolved correctly. https://swagger.io/docs/specification/serialization/#query Still looking for a solution without implementing a custom arg resolver – Matthias Wiedemann Feb 11 '22 at 07:39

0 Answers0