简单使用
@ApiOperation( value = "根据实施清单编码办理项查询" )
@PostMapping( "getTransactPageList" )
@ApiImplicitParams( {
@ApiImplicitParam( name = "page", value = "页码", paramType = "query", dataType = "String" ),
@ApiImplicitParam( name = "limit", value = "数量", paramType = "query", dataType = "String" ),
@ApiImplicitParam( name = "taskCode", value = "实施清单编码", paramType = "query", dataType = "String" ),
@ApiImplicitParam( name = "transactName", value = "办理项名称,默认为空", paramType = "query", dataType = "String" )
} )
public Object getTransactPageList( @NotBlank(message = "页码不能为空") String page,
@NotBlank(message = "每页数目不能为空") String limit,
@NotBlank(message = "实施清单编码不能为空") String taskCode,
String transactName )
{
return(zwwPhaseService.getTransactPageList( page, limit, taskCode, transactName ) );
}
在实体类中使用
@ApiModel( value = "根据实施清单编码办理项查询参数" )
@Data
@Accessors( chain = true )
public class ProjectPageListDto {
@NotBlank( message = "页数不能为空" ) /* 在实体属性上标注校验 */
@ApiModelProperty( value = "页数" )
private String page;
@NotBlank( message = "每页数目不能为空" )
@ApiModelProperty( value = "每页数目" )
private String limit;
@ApiModelProperty( value = "部门编码" )
private String deptCode;
@ApiModelProperty( value = "实施清单父级编码" )
private String parentCode;
@ApiModelProperty( value = "实施清单名称,模糊查询,默认为空" )
private String taskName;
@ApiModelProperty( value = "事项主题" )
private String subject;
@ApiModelProperty( value = "网办深度" )
private String networkDepth;
@ApiModelProperty( value = "面向对象1:自然人2:法人" )
private String serviceCode;
@NotBlank( message = "所属区划不能为空" )
@ApiModelProperty( value = "所属区划" )
private String regionCode;
@ApiModelProperty( value = "事项类型" )
private String taskType;
}
/* 在接收参数前标注@Validated */
@ApiOperation( value = "根据部门等条件查询实施清单" )
@PostMapping( "getProjectPageList" )
public Object getProjectPageList( @Validated ProjectPageListDto projectPageListDto )
{
return(zwwPhaseService.getProjectPageList( projectPageListDto ) );
}
注意,使用时需要在controller头部加上@Validated注解,否则不会生效
统一处理校验异常
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {
@ResponseBody
@ExceptionHandler(ConstraintViolationException.class)
public Response handleConstraintViolationException(ConstraintViolationException exception){
String msg = exception.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
Response response = new Response();
return response.setCode(StatusCode.PARAM_ERROR.getCode()).setMessage(msg);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Response handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
String msg = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
return new Response().setCode(StatusCode.PARAM_ERROR.getCode()).setMessage(msg);
}
@ExceptionHandler(BindException.class)
@ResponseBody
public Response handleBindException(BindException e){
String msg = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
return new Response().setCode(StatusCode.PARAM_ERROR.getCode()).setMessage(msg);
}
}
遇到一个错误就抛出
有时候我们并不想等所有的都校验完返回一堆错误,而是想让它在碰到一个校验错误时就直接抛出,可以如下配置
自定义配置类 ValidatorConfig
package com.zz.spxt.component.config;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
/**
* @Author: yang
* @Date: Create in 2020/6/17
* @Description:
* @Modify By:
*/
@Configuration
public class ValidatorConfig {
@Bean
public Validator validator(){
ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
.configure()
//failFast的意思只要出现校验失败的情况,就立即结束校验,不再进行后续的校验。
.failFast(true)
.buildValidatorFactory();
return validatorFactory.getValidator();
}
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
methodValidationPostProcessor.setValidator(validator());
return methodValidationPostProcessor;
}
}
欢迎评论和留言