1、定义接收参数DTO
@Data
@ApiModel(value = "DataSettingDTO",description = "字段配置属性信息")
public class DataSettingDTO extends BaseDTO implements Serializable {
private static final long serialVersionUID=1L;
@ApiModelProperty(value="业务主键ID")
private Integer id;
/**
* 字段类型
*/
@ApiModelProperty(value="字段类型")
private String type;
/**
* 字段值
*/
@Size(min = 1,max = 20,message = "字段值长度范围必须在1~40")
@ApiModelProperty(value="字段值")
private String name;
/**
* 字段描述
*/
@Size(max = 200,message = "字段描述长度范围必须在1~200")
@ApiModelProperty(value="段描述")
private String remark;
/**
* 创建人
*/
@ApiModelProperty(value="创建人ID")
private Integer creator;
/**
* 是否删除0:是,1:否
*/
@ApiModelProperty(value="是否删除0:是,1:否")
private Integer isDelete;
}
2、参数入口进行校验
@Controller
@RequestMapping("/xxx/dataSetting")
@Api(tags = "配置功能_字段")
public class DataSettingController {
@Autowired
private DataSettingBusiness dataSettingBusiness;
@RequestMapping(method =RequestMethod.POST,value = "save")
@ResponseBody
@ApiOperation(value = "保存字段信息",notes = "")
public Response<?> saveDataSettingById(@Valid @ModelAttribute DataSettingDTO dataSettingDTO){
return dataSettingBusiness.saveDataSetting(dataSettingDTO);
}
@RequestMapping(method =RequestMethod.POST,value = "update")
@ResponseBody
@ApiOperation(value = "修改编辑字段信息",notes = "")
public Response<?> updateDataSettingById(@Valid @ModelAttribute DataSettingDTO dataSettingDTO){
return dataSettingBusiness.updateDataSetting(dataSettingDTO);
}
}
3、定义全局错误拦截器
@RestControllerAdvice
@Slf4j
public class GlobErrorHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public Response<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException notValidException){
StringBuilder errorInfo = new StringBuilder();
notValidException.getBindingResult().getFieldErrors().forEach(fieldError->{
if(errorInfo.length()>0){
errorInfo.append(",");
}
errorInfo.append(fieldError.getDefaultMessage());
});
log.error(notValidException.getMessage(),notValidException);
return ResponseUtils.returnCommonException(errorInfo.toString());
}
@ExceptionHandler(ConstraintViolationException.class)
public Response<?> handleConstraintViolationException(ConstraintViolationException constraintViolationException){
StringBuilder errorInfo = new StringBuilder();
constraintViolationException.getConstraintViolations().forEach(violation->{
if(errorInfo.length()>0){
errorInfo.append(",");
}
errorInfo.append(violation.getMessage());
});
log.error(constraintViolationException.getMessage(),constraintViolationException);
return ResponseUtils.returnCommonException(errorInfo.toString());
}
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Response<?> handOtherException(BindException bindException){
StringBuilder errorInfo = new StringBuilder();
bindException.getBindingResult().getFieldErrors().forEach(fieldError->{
if(errorInfo.length()>0){
errorInfo.append(",");
}
errorInfo.append(fieldError.getDefaultMessage());
});
log.error(bindException.getMessage(),bindException);
return ResponseUtils.returnCommonException(errorInfo.toString());
}
}