Marty Pitt
Marty Pitt

Reputation: 29280

Spring MVC - Is it possible to receive a strongly typed request object on a GET?

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?

Upvotes: 0

Views: 674

Answers (1)

Marty Pitt
Marty Pitt

Reputation: 29280

It seems the answer was to simply remove the annotation.

This worked:

@RequestMapping(method=RequestMethod.GET)
public @ResponseBody List<Result> search(SearchRequest request) {}

Upvotes: 2

Related Questions