简体   繁体   English

Spring请求参数的自定义验证

[英]Custom validation of Spring request parameter

I've exposed a rest service that accepts value_param as a request parameter.我公开了一个接受value_param作为请求参数的休息服务。 If the url param is not spelt correctly then the following exception is thrown:如果 url 参数拼写不正确,则会引发以下异常:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Sep 15 10:57:54 IST 2020
There was an unexpected error (type=Bad Request, status=400).
Required String parameter 'value_param' is not present

I'm attempting to explicitly check the parameter.我正在尝试明确检查参数。 This parameter check is in combination with other checks and any validation check fails, throw a 422 error.此参数检查与其他检查结合使用,任何验证检查失败,都会抛出 422 错误。

Here is the endpoint signature:这是端点签名:

   @GetMapping("/values")
    public List<ValueEntity> getValues(@RequestParam(name = "value_param") String valueParameter) {

How to access the value_param parameter and prevent the Error page with status 400 being thrown ?如何访问value_param参数并防止抛出状态为 400 的错误页面?

Mark the field as @RequestParam(required = false) in the controller.在控制器中将该字段标记为@RequestParam(required = false)
For more detailed examples could be seen: here可以看到更详细的例子: here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM