国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn)

瀏覽:95日期:2023-04-14 08:51:07

依賴添加

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId></dependency>

一些較老版本的SpringBoot需要添加相關(guān)依賴,我使用的2.1.4發(fā)行版不用這個(gè)操作。

驗(yàn)證使用對(duì)象接收參數(shù)的情況

public class PointDeductSetRequest { private Long id; @NotBlank(message = '租戶id為空') private String tenantId; private Integer status; @NotNull private Integer pointValue; @NotNull private Integer deductValue; @NotBlank(message = '操作員id為空') private String operator;}

首先在需要驗(yàn)證的對(duì)象的對(duì)應(yīng)字段上方加上校驗(yàn)注解,以下為一些常用注解:

@Null 限制只能為null @NotNull 限制必須不為null @AssertFalse 限制必須為false @AssertTrue 限制必須為true @DecimalMax(value) 限制必須為一個(gè)不大于指定值的數(shù)字 @DecimalMin(value) 限制必須為一個(gè)不小于指定值的數(shù)字 @Digits(integer,fraction) 限制必須為一個(gè)小數(shù),且整數(shù)部分的位數(shù)不能超過(guò)integer,小數(shù)部分的位數(shù)不能超過(guò)fraction @Future 限制必須是一個(gè)將來(lái)的日期 @Max(value) 限制必須為一個(gè)不大于指定值的數(shù)字 @Min(value) 限制必須為一個(gè)不小于指定值的數(shù)字 @Past 限制必須是一個(gè)過(guò)去的日期 @Pattern(value) 限制必須符合指定的正則表達(dá)式 @Size(max,min) 限制字符長(zhǎng)度必須在min到max之間 @Past 驗(yàn)證注解的元素值(日期類型)比當(dāng)前時(shí)間早 @NotEmpty 驗(yàn)證注解的元素值不為null且不為空(字符串長(zhǎng)度不為0、集合大小不為0) @NotBlank 驗(yàn)證注解的元素值不為空(不為null、去除首位空格后長(zhǎng)度為0),不同于@NotEmpty,@NotBlank只應(yīng)用于字符串且在比較時(shí)會(huì)去除字符串的空格 @Email 驗(yàn)證注解的元素值是Email,也可以通過(guò)正則表達(dá)式和flag指定自定義的email格式

@RequestMapping(value = '/deduct', method = RequestMethod.POST)public BusinessResponse setPointDeduct(@RequestBody @Valid PointDeductSetRequest request){ pointDeductService.setPointDeductRule(request); return new BusinessResponse(ResponseEnum.OK);}

之后在controller方法的對(duì)象參數(shù)前加@Valid注解。

校驗(yàn)使用單個(gè)參數(shù)接受的情況

@RequestMapping(value = '/deduct', method = RequestMethod.GET)public PageResponse<TPointDeduct> getPointDeductList(@RequestParam(value = 'page', required = false) Integer page, @RequestParam(value = 'pageSize', required = false) Integer pageSize, @RequestParam(value = 'tenantId', required = false) @NotBlank(message = '租戶id為空') String tenantId, @RequestParam(value = 'status', required = false) Integer status){ PageResponse<TPointDeduct> response = pointDeductService.getPointDeductList(page, pageSize, tenantId, status); response.setCodeMsg(ResponseEnum.OK); return response;}

首先需要在controller類上加@Validated注解,之后在方法中需要校驗(yàn)的參數(shù)前加上對(duì)應(yīng)的校驗(yàn)注解進(jìn)行校驗(yàn)。

對(duì)校驗(yàn)產(chǎn)生的異常的捕獲

定義全局異常處理類并用@ControllerAdvice標(biāo)注,由于對(duì)象和單個(gè)參數(shù)因校驗(yàn)產(chǎn)生的異常類型不同,因此需要分別處理。

對(duì)于對(duì)象作為接收前端請(qǐng)求的情況,因校驗(yàn)產(chǎn)生的異常類型為MethodArgumentNotValidException,示例方法如下:

/** * 捕獲303對(duì)于body中的對(duì)象字段校驗(yàn) * @param e * @param request * @return */@ExceptionHandler(MethodArgumentNotValidException.class)@ResponseBodyResponseEntity<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request){ List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); if (fieldErrors != null && !fieldErrors.isEmpty()){ String message = fieldErrors.get(0).getDefaultMessage(); log.error(message, e); } HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; HttpHeaders headers = new HttpHeaders(); Response response = new Response(); response.setCode(ResponseEnum.FORMAT_ERROR.code()); response.setMessage(ResponseEnum.FORMAT_ERROR.message()); return new ResponseEntity<>(response, headers, httpStatus);}

對(duì)于使用單個(gè)參數(shù)接受前端請(qǐng)求,因校驗(yàn)產(chǎn)生的異常類為ConstraintViolationException,示例方法如下:

/** * 捕獲303對(duì)于request param單個(gè)參數(shù)的校驗(yàn) * @param e * @param request * @return */@ExceptionHandler(ConstraintViolationException.class)@ResponseBodyResponseEntity<Object> handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request){ HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; HttpHeaders headers = new HttpHeaders(); Response response = new Response(); response.setCode(ResponseEnum.FORMAT_ERROR.code()); response.setMessage(ResponseEnum.FORMAT_ERROR.message()); return new ResponseEntity<>(response, headers, httpStatus);}

到此這篇關(guān)于SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot jsr303校驗(yàn)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 舞阳县| 苏尼特右旗| 自贡市| 北碚区| 英超| 泗水县| 卓尼县| 特克斯县| 阳谷县| 富民县| 响水县| 壤塘县| 东乡族自治县| 龙里县| 阳朔县| 揭阳市| 神木县| 定襄县| 商都县| 阿图什市| 五峰| 梧州市| 泰兴市| 沧州市| 资兴市| 大同县| 巴中市| 乌兰浩特市| 贵德县| 高青县| 图片| 盐边县| 朔州市| 二连浩特市| 苍南县| 武宣县| 浮山县| 朝阳市| 普格县| 南漳县| 灵丘县|