@Validated和@Valid的区别
@Validated和@Valid的区别
注解位置
@Validated:可以用在类型、方法和方法参数上。但是不能用在成员属性(字段)上
@Valid:可以用在方法、构造函数、方法参数和成员属性(字段)上
分组功能
-
@Valid:作为标准JSR-303规范,还没有吸收分组的功能。
-
@Validated:提供了一个分组功能,可以在入参验证时,根据不同的分组采用不同的验证机制。没有添加分组属性时,默认验证没有分组的验证属性。
-
以下着重说明使用分组方法
@Valid使用demo
Controller层
package com.medical.platform.biz.pay.controller;
import com.medical.platform.biz.base.Response;
import com.medical.platform.biz.pay.service.PayInfoService;
import com.medical.platform.biz.pay.vo.PayInfoReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* <p>
* 缴费信息表 前端控制器
* </p>
*
* @author gjp
* @since 2022-10-17
*/
@RestController
@RequestMapping("/pay")
@Api(tags = "缴费模块")
public class PayInfoController {
@Autowired
PayInfoService payInfoService;
@GetMapping(value = "/getInfoList")
@ApiOperation(value = "查询缴费列表",notes = "获取就诊人缴费列表")
public Response getPayInfo(@Valid PayInfoReqVo payInfoReqVo){
return Response.success(payInfoService.getPayInfoRespVoList(payInfoReqVo));
}
}
entity层
package com.medical.platform.biz.pay.vo;
import com.medical.platform.biz.pay.validate.PayInfoReqGroup;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* @Author gjp
* @Date Created in 2022/10/17 10:23
* @Desc
*/
@Data
@ApiModel("获取缴费信息请求参数")
public class PayInfoReqVo {
/**
* 患者编号
*/
@NotBlank(message = "患者编号不能为空")
private String patientCode;
/**
* 主订单号
*/
private String payOrderId;
/**
* 查询已缴费/未缴费 false是未缴费,true是已缴费列表
*/
@NotNull(message = "缴费列表转换值不能为空!")
private Boolean payFlag;
}
@Validated使用demo
注:该注解提供了分组校验,也就是提供了,同一参数请求体支持不同方法进行校验
@Validated(Default.class) 只验证参数分组中包含Default.class分组的参数,若其他方法进行验证,只需对参数进行分组即可
Controller层
package com.medical.platform.biz.pay.controller;
import com.medical.platform.biz.base.Response;
import com.medical.platform.biz.pay.service.PayInfoService;
import com.medical.platform.biz.pay.service.impl.PayInfoServiceImpl;
import com.medical.platform.biz.pay.vo.PayInfoReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import javax.validation.groups.Default;
/**
* <p>
* 缴费信息表 前端控制器
* </p>
*
* @author gjp
* @since 2022-10-17
*/
@RestController
@RequestMapping("/pay")
@Api(tags = "缴费模块")
public class PayInfoController {
@Autowired
PayInfoService payInfoService;
@GetMapping(value = "/getInfoList")
@ApiOperation(value = "查询缴费列表",notes = "获取就诊人缴费列表")
public Response getPayInfo(@Validated(Default.class) PayInfoReqVo payInfoReqVo){
return Response.success(payInfoService.getPayInfoRespVoList(payInfoReqVo));
}
}
entity层
package com.medical.platform.biz.pay.vo;
import com.medical.platform.biz.pay.validate.PayInfoReqGroup;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
/**
* @Author gjp
* @Date Created in 2022/10/17 10:23
* @Desc
*/
@Data
@ApiModel("获取缴费信息请求参数")
public class PayInfoReqVo {
/**
* 患者编号
*/
@NotBlank(message = "患者编号不能为空",groups = PayInfoReqGroup.GetPayInfo.class)
private String patientCode;
/**
* 主订单号
*/
private String payOrderId;
/**
* 查询已缴费/未缴费 false是未缴费,true是已缴费列表
*/
@NotNull(message = "缴费列表转换值不能为空!",groups =Default.class)
private Boolean payFlag;
}