简体   繁体   中英

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?

It seems the answer was to simply remove the annotation.

This worked:

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

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