简体   繁体   English

是否可以在 Spring MVC REST 端点中验证 @RequestParam?

[英]Is it possible to validate @RequestParam in Spring MVC REST endpoint?

In Jersey 2 it is possible to do this:在 Jersey 2 中可以这样做:

@GET
@PATH("user/{email}")
public IDto getUser(@NotNull @Email @PathParam("email") String validEmail) {
    return userManagementService.findUserByEmail(validEmail);
}

But I cannot make something similar to work in Spring MVC, it seems that the validation is only done when providing an object in @RequestBody or using an SpringMVC Form, for example the following won't work:但是我无法在 Spring MVC 中进行类似的工作,似乎只有在 @RequestBody 中提供对象或使用 SpringMVC 表单时才会进行验证,例如以下内容不起作用:

@RequestMapping(value="/user/{email}", method = RequestMethod.GET)
public @ResponseBody IDto getUser(@NotNull @Email @PathVariable String validEmail) {
    return userManagementService.findUserByEmail(validEmail);
}   

There are other similar questions, but those seem to be oriented to Spring MVC UI applications, in my case it is only a REST API which returns JSON response so I don't have any View to map/bind to the controller.还有其他类似的问题,但这些问题似乎是针对 Spring MVC UI 应用程序的,在我的情况下,它只是一个返回 JSON 响应的 REST API,所以我没有任何视图可以映射/绑定到控制器。

Seems it is possible, using @Validated .似乎有可能,使用@Validated

Here's an example .这是一个例子

As far as I can tell, you cannot do this out-of-the-box with Spring.据我所知,你不能用 Spring 开箱即用。

Options:选项:

  1. Use a regular expression:使用正则表达式:

     @RequestMapping(value="/user/{email:SOME_REXEXP}", method = RequestMethod.GET) public @ResponseBody IDto getUser(@PathVariable String validEmail) { return userManagementService.findUserByEmail(validEmail); }
  2. Use Hibernate Validator to validate the method.使用 Hibernate Validator 来验证方法。 Either call the validator manually, or make Spring call it for you using AOP.要么手动调用验证器,要么让 Spring 使用 AOP 为您调用它。 See https://github.com/gunnarmorling/methodvalidation-integrationhttps://github.com/gunnarmorling/methodvalidation-integration

1- Simply add @Validated annotation at the top of your class. 1- 只需在类的顶部添加 @Validated 注释。 2- Put whatever annotations for validations (@NotBlank, Min(1), etc.) before the @RequestParam annotation in your method signature. 2- 在方法签名中的 @RequestParam 注释之前放置任何用于验证的注释(@NotBlank、Min(1) 等)。

Controller should be annotated with spring's @Validated控制器应使用 spring 的@Validated进行注释

So update your code with所以更新你的代码

@Validated
@RequestMapping(value="/user/{email}", method = RequestMethod.GET)
public @ResponseBody IDto getUser(
    @NotNull 
    @Email 
    @PathVariable String validEmail) {
    return userManagementService.findUserByEmail(validEmail);
}

The validated annotation from the org.springframework.validation.annotation.Validated package to validate a @PathVariable .来自org.springframework.validation.annotation.Validated包的经过验证的注解来验证@PathVariable Make sure the class annotated with @Validated .确保使用@Validated注释的类。

@GetMapping("/name-for-day/{dayOfWeek}")
  public String getNameOfDay(@PathVariable("dayOfWeek") @Min(1) @Max(7) Integer dayOfWeek) {
        return dayOfWeek + "";
}

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

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