简体   繁体   中英

How to pass a json object as a parameter in a REST request URL with Springboot

I have looked at the various answers and they do not resolve my issue. I have a very specific client need where I cannot use the body of the request.

I have checked these posts:

Note: I do encode the URI.

I get various errors but illegal HTML character is one. The requirement is quite simple:

Write a REST service which accepts the following request

GET /blah/bar?object=object11&object=object2&...

object is a POJO that will come in the following JSON format

{
    "foo": bar,  
    "alpha": {       
        "century": a,
    }
}

Obviously I will be reading in a list of object...

My code which is extremely simplified... as below.

 @RequestMapping(method=RequestMethod.GET, path = "/test")
 public Greeting test(@RequestParam(value = "object", defaultValue = "World") FakePOJO aFilter) {
     return new Greeting(counter.incrementAndGet(), aFilter.toString());
 }

I have also tried to encapsulate it as a String and convert later which doesnt work either.

Any suggestions? This should really be extremely simple and the hello world spring rest tut should be a good dummy test framework.

---- EDIT ----

I have figured out that there is an underlying with how jackson is parsing the json. I have resolved it but will be a write up.. I will provide the exact details after Monday. Short version. To make it work for both single filter and multiple filters capture it as a string and use a json slurper

If you use @RequestParam annotation to a Map<String, String> or MultiValueMap<String, String> argument, the map will be populated with all request parameters you specified in the URL.

 @GetMapping("/blah/bar")
 public Greeting test(@RequestParam Map<String, String> searchParameters) {
     ...
 }

check the documentation for a more in depth explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM