服務端介面引數校驗
阿新 • • 發佈:2021-01-22
技術標籤:web專案開發
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; @Override public String toString() { return "DataSettingDTO{" + "id=" + id + ", type='" + type + '\'' + ", name='" + name + '\'' + ", remark='" + remark + '\'' + ", creator=" + creator + ", isDelete=" + isDelete + '}'; } }
2、引數入口進行校驗
@Controller @RequestMapping("/xxx/dataSetting") @Api(tags = "配置功能_欄位") public class DataSettingController { @Autowired private DataSettingBusiness dataSettingBusiness; @RequestMapping(method =RequestMethod.GET,value = "select") @ResponseBody @ApiOperation(value = "欄位詳情",notes = "") public Response<?> selectDataSettingById(@ModelAttribute DataSettingDTO dataSettingDTO){ return dataSettingBusiness.selectDataSettingById(dataSettingDTO); } @RequestMapping(method =RequestMethod.POST,value = "save") @ResponseBody @ApiOperation(value = "儲存欄位資訊",notes = "") public Response<?> saveDataSettingById(@Valid @ModelAttribute DataSettingDTO dataSettingDTO){ return dataSettingBusiness.saveDataSetting(dataSettingDTO); } @RequestMapping(method =RequestMethod.GET,value = "delete") @ResponseBody @ApiOperation(value = "刪除欄位資訊",notes = "") public Response<?> deleteDataSettingById(@ModelAttribute DataSettingDTO dataSettingDTO){ dataSettingBusiness.deleteDataSetting(dataSettingDTO); return ResponseUtils.returnSuccess(); } @RequestMapping(method =RequestMethod.POST,value = "update") @ResponseBody @ApiOperation(value = "修改編輯欄位資訊",notes = "") public Response<?> updateDataSettingById(@Valid @ModelAttribute DataSettingDTO dataSettingDTO){ return dataSettingBusiness.updateDataSetting(dataSettingDTO); } @RequestMapping(method =RequestMethod.GET,value = "list") @ResponseBody @ApiOperation(value = "欄位資訊列表頁獲取",notes = "") public Response<?> DataSettingList(@ModelAttribute DataSettingDTO dataSettingDTO){ return ResponseUtils.returnApiObjectSuccess(dataSettingBusiness.dataSettingList(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()); } }