服务端接口参数校验

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());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山不在高_有仙则灵

你的奖励是我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值