欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

@Valid和@Validated注解校验List<Object>

程序员文章站 2022-07-05 16:44:56
@Valid和@Validated注解校验List< Object>spring 4整合hibernate-validator61.导包 org.hibernate.validator hibernate-validator

@Valid和@Validated注解校验List< Object>

spring 4整合hibernate-validator6

1.导包

		<dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml</groupId>
            <artifactId>classmate</artifactId>
            <version>1.5.0</version>
        </dependency>

2.实现List< E >接口

/*
 * Copyright (c) 2005, 2020, EVECOM Technology Co.,Ltd. All rights reserved.
 * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package net.evecom.styqwebsite.util;


import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.*;

/**
 * 描述
 *
 * @author Jerome
 * @created 2021/1/19 14:43
 */
public class ValidableList<E> implements List<E> {

    @Valid
    @NotEmpty(message = "参数不能为空")
    private List<E> list = new ArrayList<>();

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

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

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

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

    @Override
    public boolean contains(Object o) {
        return list.contains(o);
    }

    @Override
    public Iterator<E> iterator() {
        return list.iterator();
    }


    @Override
    public Object[] toArray() {
        return list.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return list.toArray(a);
    }

    @Override
    public boolean add(E e) {
        return list.add(e);
    }

    @Override
    public boolean remove(Object o) {
        return list.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return list.containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return list.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return list.addAll(index, c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return list.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return list.retainAll(c);
    }

    @Override
    public void clear() {
        list.clear();
    }

    @Override
    public E get(int index) {
        return list.get(index);
    }

    @Override
    public E set(int index, E element) {
        return list.set(index, element);
    }

    @Override
    public void add(int index, E element) {
        list.add(index, element);
    }

    @Override
    public E remove(int index) {
        return list.remove(index);
    }

    @Override
    public int indexOf(Object o) {
        return list.indexOf(o);
    }

    @Override
    public int lastIndexOf(Object o) {
        return list.lastIndexOf(o);
    }

    @Override
    public ListIterator<E> listIterator() {
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }


    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return list.subList(fromIndex, toIndex);
    }
}

3.实体类

package com.atguigu.spring.autowire;

import javax.validation.constraints.NotEmpty;

public class Dept {
    
    @NotEmpty(message = "名称不能为空")
    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getDname() {
        return dname;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "dname='" + dname + '\'' +
                '}';
    }
}

4.控制层

/*
 * Copyright (c) 2005, 2020, EVECOM Technology Co.,Ltd. All rights reserved.
 * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package net.evecom.styqwebsite.action;

import net.evecom.common.action.BaseController;
import net.evecom.common.util.ValidateList;
import net.evecom.common.vmodel.VReturnMessage;
import net.evecom.styqwebsite.model.EarthquakeAreaDesc;
import net.evecom.styqwebsite.service.EarthquakeAreaDescService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
 
@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {
    
    @Autowired
    private DeptService deptService;
    
    /*
    @Valid和@Validated注解都可以用
    */
    @PostMapping("/save")
    public VReturnMessage save(@RequestBody @Valid ValidateList<Dept> deptList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            StringBuffer errors = new StringBuffer();
            bindingResult.getAllErrors().forEach(error ->{
                errors.append(error.getDefaultMessage() + ";");
            });
            return VReturnMessage.fail(errors.toString());
        }
        try {
            deptService.save(deptList);
        } catch (Exception exception) {
            LOGGER.error("错误",exception);
            return VReturnMessage.fail("保存出错");
        }
        return VReturnMessage.success();
    }
}

本文地址:https://blog.csdn.net/qq_43701330/article/details/112861538