[英]Spring GET method how to raise Exception if an unknown request param in controller
So I have in my controller a GET method with optional request params:所以我在我的 controller 中有一个带有可选请求参数的 GET 方法:
@GetMapping(path = "/users/search")
public @ResponseBody ResponseEntity<List<User>> getUserLike(
@RequestParam(name = "id", required = false) Long id,
@RequestParam(name = "name", required = false) String name,
@RequestParam(name = "dateofbirth", required = false) @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate dateOfBirth
) {
return userService.getUserLike(id, name, dateOfBirth);
}
When I try to call this request with unknown parameters当我尝试使用未知参数调用此请求时
/users/search?id=1&myunknownparam=unknownParam
I'd like to raise an Exception when request has unknown param such as myunknownparam here.当请求具有未知参数(例如此处的myunknownparam )时,我想引发异常。
Though for now as all my parameters are optional my service returns the same result as it would with all the parameters set to null.虽然目前我的所有参数都是可选的,但我的服务返回的结果与将所有参数设置为 null 时的结果相同。
Query param are often optional, so it's rare that a server could reject you because of that.查询参数通常是可选的,因此服务器很少会因此拒绝您。
But what you are trying to achieve can be implemented with an Interceptor, you need some reflection to get the parameters of your method and verify that each client param is defined on the signature of your method.但是您尝试实现的目标可以使用拦截器来实现,您需要一些反射来获取方法的参数并验证每个客户端参数是否定义在方法的签名上。
Something like:就像是:
@Component
public class QueryParamInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) {
Map<String, String[]> clientRequestParameters = request.getParameterMap();
MethodParameter[] methodParameters = ((HandlerMethod) handler).getMethodParameters();
List<String> definedMethodsParameters = Arrays.stream(methodParameters)
.flatMap(methodParameter -> Arrays.stream(methodParameter.getParameterAnnotations()))
.filter(annotation -> annotation.annotationType().isAssignableFrom(RequestParam.class))
.map(annotation -> ((RequestParam) annotation).name())
.collect(Collectors.toList());
for (String clientRequestParameterKey : clientRequestParameters.keySet()) {
if (!definedMethodsParameters.contains(clientRequestParameterKey)) {
throw new IllegalArgumentException("The parameter " + clientRequestParameterKey + " passed by the client is unknown");
}
}
return true;
}
}
Note that an interceptor will be called at each request.请注意,每个请求都会调用一个拦截器。 So I would not use that in production due to the high cost of object introspection..
所以我不会在生产中使用它,因为 object 内省的成本很高。
@GetMapping(path = "/users/search")
public @ResponseBody
ResponseEntity<List<User>> getUserLike(
@RequestParam Map<String, String> allParams,
@RequestParam(name = "id", required = false) Long id,
@RequestParam(name = "name", required = false) String name,
@RequestParam(name = "dateofbirth", required = false) @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate dateOfBirth
) {
// here check if allParams contains only valid keys
return userService.getUserLike(id, name, dateOfBirth);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.