@Valid和@Validated在service层的分组校验

service 接口代码

import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;

@Validated
public interface TestService { 
@Validated({DIY.Add.class, Default.class})
 R funOne(@Valid  objectDTO one );

@Validated({DIY.Edit.class})
 R funList(@Valid  List<objectDTO> list );

}

DIY 是自己定义的分组类

public interface DIY{ 
	interface Add {}
	interface Edit {}
}

校验规则定义

public class  ObjectDTO {
@NotNull(message = "id不为空",groups = {DIY.Edit.class})
private Long id;

@NotEmpty(message = "name不为空",groups = {DIY.Add.class})
private String name;

@NotNull(message = "时间不为空(默认分组 Default.class)")
private Date date;
}

你可能感兴趣的:(Java项目,开发语言)