[英]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. 我有一个Spring MVC控制器,它正在为GET请求提供服务,以执行搜索。
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
. 如果我使用POST或PUT,我可以使用@RequestBody
很好地编组入站请求。 However, because I'm using a GET, this doesn't seem to fit. 但是,因为我正在使用GET,所以这似乎不合适。
Instead, it seems I'm required to list all the possible parameters on the method signature as @RequestParam(required=false)
. 相反,似乎我需要将方法签名上的所有可能参数列为@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. 除了导致丑陋的方法签名之外,我还因为不在这里使用类而失去了各种OO的优点。
Attempting to use @RequestBody
fails (understandably so), and as discussed here and here , using an actual request body on a GET is not desirable. 尝试使用@RequestBody
失败(可以理解),并且正如这里和此处所讨论的, 在 GET上使用实际请求主体是不可取的。
Is there a way to get Spring MVC to support marshalling multiple @RequestParam
's to a strongly typed object on GET requests? 有没有办法让Spring MVC支持将多个@RequestParam
编组到GET请求上的强类型对象?
It seems the answer was to simply remove the annotation. 似乎答案是简单地删除注释。
This worked: 这有效:
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody List<Result> search(SearchRequest request) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.