使用Bean Validation 2.0(JSR-308)和Spring 5验证bean列表 [英] Validating a List of beans using Bean Validation 2.0 (JSR-308) and Spring 5

查看:133
本文介绍了使用Bean Validation 2.0(JSR-308)和Spring 5验证bean列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 5应该支持Bean Validation 2.0,该版本引入了对List类型的验证,但是我似乎无法让它对我有用.

我的@RestController中具有以下端点:

@PostMapping("/foos")
public List<Foo> analysePoints(@RequestBody @Valid List<@Valid Foo> request) {
    ...
}

Foo类的属性上有一些JSR验证注释.

不幸的是,这似乎根本没有验证它.在Spring 5之前,我通过将List<Foo>包装在单独的请求bean对象中来解决此问题,在这种情况下也可以使用,但是我不想这样做,因为它会更改所需json的格式./p>

有什么办法可以使它正常工作吗?

解决方案

javax.validation不会验证列表,仅验证JavaBean.解决方法是使用自定义的List类:

代替

@RequestBody @Valid List<@Valid Foo> request

使用:

@RequestBody @Valid ValidList<@Valid Foo> request

有效列表

public class ValidList<E> implements List<E> {

    @Valid
    private List<E> list;

    public ValidList() {
        this.list = new ArrayList<E>();
    }

    public ValidList(List<E> list) {
        this.list = list;
    }

    // Bean-like methods, used by javax.validation but ignored by JSON parsing

    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
    }

    // List-like methods, used by JSON parsing but ignored by javax.validation

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    // Other list methods ...
}

Spring 5 is supposed to support Bean Validation 2.0 which introduced validation on List types but I can't seem to get it to work for me.

I have the following endpoint in my @RestController:

@PostMapping("/foos")
public List<Foo> analysePoints(@RequestBody @Valid List<@Valid Foo> request) {
    ...
}

Where the class Foo has some JSR validation annotations on its properties.

Unfortunately, this doesn't seem to validate it at all. Prior to Spring 5 I got around this by wrapping the List<Foo> in a separate request bean object and this does work in this case too but I don't want to have to do this because it changes the format of the json required.

Is there any way to get this working?

解决方案

javax.validation won't validate a list, only a JavaBean. The workaround is to use a customized List class:

Instead of

@RequestBody @Valid List<@Valid Foo> request

Use:

@RequestBody @Valid ValidList<@Valid Foo> request

ValidList

public class ValidList<E> implements List<E> {

    @Valid
    private List<E> list;

    public ValidList() {
        this.list = new ArrayList<E>();
    }

    public ValidList(List<E> list) {
        this.list = list;
    }

    // Bean-like methods, used by javax.validation but ignored by JSON parsing

    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
    }

    // List-like methods, used by JSON parsing but ignored by javax.validation

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    // Other list methods ...
}

这篇关于使用Bean Validation 2.0(JSR-308)和Spring 5验证bean列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆