简体   繁体   English

Spring MVC始终将@Valid应用于特定参数对象的所有映射

[英]Spring MVC always apply @Valid for all mappings for particular parameter object

Let's say I would like to validate incoming ID parameter for all my RESTful methods (>50). 假设我想验证所有RESTful方法(> 50)的传入ID参数。

As an example I have: 例如,我有:

@RequestMapping(
        value = "/{id}",
        method = RequestMethod.GET,
        produces = {"application/json"})
@ResponseStatus(HttpStatus.OK)
public
@ResponseBody
Metadata getMetadata(
        @PathVariable("id") Long id,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    return metadataService.get(id);
}

I would like to reject all requests if id < 1. As a solution I've implemented: 如果ID <1,我想拒绝所有请求。作为一种解决方案,我实现了:

@RequestMapping(
        value = "/{id}",
        method = RequestMethod.GET,
        produces = {"application/json"})
@ResponseStatus(HttpStatus.OK)
public
@ResponseBody
Metadata getMetadata(
        @Valid Id id,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    return metadataService.get(id.asLong());
}

public class Id {

@Min(1)
@NotNull
private Long id;

public void setId(Long id) {
    this.id = id;
}

public Long asLong() {
    return id;
}

}

But now I have to implicitly put @Valid annotation for each and every method for Id argument, which seems quite redundant . 但是现在我必须为ID参数的每个方法隐式地添加@Valid批注,这似乎很多余。 Is there a way to tell Spring that if there's an Id object as an incoming parameter it should always be @Valid. 有没有办法告诉Spring,如果有一个Id对象作为传入参数,则应始终为@Valid。 Without putting annotation each time? 每次都没有放置注释?

Thanks. 谢谢。

So I've ended up with solution like this: 所以我最终得到了这样的解决方案:

public class CustomModelAttributeMethodProcessor extends ModelAttributeMethodProcessor {

public CustomModelAttributeMethodProcessor(boolean annotationNotRequired) {
    super(annotationNotRequired);
}

@Override
protected void bindRequestParameters(final WebDataBinder binder, final NativeWebRequest request) {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    ((ServletRequestDataBinder) binder).bind(servletRequest);
}

@Override
protected void validateIfApplicable(final WebDataBinder binder, final MethodParameter parameter) {
    if (binder.getTarget().getClass().equals(Id.class)) {
        binder.validate();
        return;
    }

    super.validateIfApplicable(binder, parameter);
}

} }

And configuration: 并配置:

@Configuration
@EnableWebMvc
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(new CustomModelAttributeMethodProcessor(true));
    }
}

A bit overhead to check class of each and every incoming parameter, but works as expected. 检查每个传入参数的类的开销有点大,但是按预期工作。 Now @Valid annotation can be omitted as validation performs by custom processor. 现在,@ Valid批注可以省略,因为自定义处理器会执行验证。

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

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