Skip to content
Advertisement

Unmarshalling query params into object using Swagger/Springfox and Spring Boot

I’ve got a search endpoint in my REST service. I’m using Spring Boot, so I’ve got a @RestController setup with a method that returns search results based on a search query. This is the method definition:

@ApiOperation(value = "Get global search results")
@RequestMapping(method = GET, produces = {"application/json"})
public SearchResults get(SearchQuery query) {
    ...
}

I was hoping that SwaggerUI would show the SearchQuery’s fields as separate query params in the ui. It doesn’t; it just shows a generic “query” param. If I annotate query with @RequestBody the user can add a json payload to the body, but this is a GET request – I want the user to only use query params.

Any ideas?

Advertisement

Answer

Annotate SearchQuery with @ModelAttribute and it should work as expected.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement