0

I have a Spring MVC controller which is servicing GET requests, to perform a search.

These requests have many optional parameters which may be passed on the query string.

For example:

@Data
public class SimpleSearchRequest implements SearchRequest {

    private String term;
    private List<Status> stati;
    @JsonDeserialize(using=DateRangeDeserializer.class)
    private Range<DateTime> dateRange;  
}

If I were using a POST or PUT for this, I could nicely marshall the inbound request using a @RequestBody. However, because I'm using a GET, this doesn't seem to fit.

Instead, it seems I'm required to list all the possible parameters on the method signature as @RequestParam(required=false).

Aside from leading to ugly method signatures, I'm also losing out on all sorts of OO goodness by not using classes here.

Attempting to use @RequestBody fails (understandably so), and as discussed here and here, using an actual request body on a GET is not desirable.

Is there a way to get Spring MVC to support marshalling multiple @RequestParam's to a strongly typed object on GET requests?

Community
  • 1
  • 1
Marty Pitt
  • 28,822
  • 36
  • 122
  • 195

1 Answers1

2

It seems the answer was to simply remove the annotation.

This worked:

@RequestMapping(method=RequestMethod.GET)
public @ResponseBody List<Result> search(SearchRequest request) {}
Marty Pitt
  • 28,822
  • 36
  • 122
  • 195