日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring校验@RequestParams和@PathVariables参数

發(fā)布時(shí)間:2023/12/9 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring校验@RequestParams和@PathVariables参数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我們?cè)趯慠est API接口時(shí)候會(huì)用到很多的@RequestParam和@PathVariable進(jìn)行參數(shù)的傳遞,但是在校驗(yàn)的時(shí)候,不像使用@RequestBody那樣的直接寫在實(shí)體類中,我們這篇文章講解一下如何去校驗(yàn)這些參數(shù)。

依賴配置


  • 要使用Java Validation API,我們必須添加validation-api依賴項(xiàng):
<dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>2.0.1.Final</version> </dependency>
  • 通過添加@Validated注解來啟用控制器中的@RequestParams和@PathVariables的驗(yàn)證:
@RestController @RequestMapping("/") @Validated public class Controller {// ... }

校驗(yàn)@RequestParam


  • 我們將數(shù)字作為請(qǐng)求參數(shù)傳遞給控制器方法
@GetMapping("/name-for-day") public String getNameOfDayByNumber(@RequestParam Integer dayOfWeek) {// ... }
  • 我們保證dayOfWeek的值在1到7之間,我們使用@Min和@Max注解
@GetMapping("/name-for-day") public String getNameOfDayByNumber(@RequestParam @Min(1) @Max(7) Integer dayOfWeek) {// ... }

任何與這些條件不匹配的請(qǐng)求都將返回HTTP狀態(tài)500,并顯示默認(rèn)錯(cuò)誤消息。

如果我們嘗試調(diào)用http://localhost:8080/name-for-day?dayOfWeek=24這將返回以下響應(yīng)信息:

There was an unexpected error (type=Internal Server Error, status=500). getNameOfDayByNumber.dayOfWeek: must be less than or equal to 7

當(dāng)然我們也可以在@Min和@Max注解后面加上message參數(shù)進(jìn)行修改默認(rèn)的返回信息。

校驗(yàn)@PathVariable


和校驗(yàn)@RequestParam一樣,我們可以使用javax.validation.constraints包中的注解來驗(yàn)證@PathVariable。

  • 驗(yàn)證String參數(shù)不是空且長度小于或等于10
@GetMapping("/valid-name/{name}") public void test(@PathVariable("name") @NotBlank @Size(max = 10) String username) {// ... }
  • 任何名稱參數(shù)超過10個(gè)字符的請(qǐng)求都會(huì)導(dǎo)致以下錯(cuò)誤消息:
There was an unexpected error (type=Internal Server Error, status=500). createUser.name:size must be between 0 and 10

通過在@Size注解中設(shè)置message參數(shù),可以覆蓋默認(rèn)消息。

其實(shí)我們可以看到校驗(yàn)@RequestParam和@PathVariable參數(shù)和我們校驗(yàn)@RequestBody方式一致,只不過一個(gè)是寫在了實(shí)體中,一個(gè)寫在了外部,當(dāng)然我們也可以將@RequestParam的參數(shù)寫入到實(shí)體類中,進(jìn)行使用@RequestParam注解進(jìn)行引入,比如我們使用一個(gè)分頁的實(shí)例

  • 分頁實(shí)體類
/*** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements. See the NOTICE file* distributed with this work for additional information* regarding copyright ownership. The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License. You may obtain a copy of the License at* <p>* http://www.apache.org/licenses/LICENSE-2.0* <p>* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/ package com.zhuanqb.param.page;import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotEmpty;import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull;/*** PageParam <br/>* 描述 : PageParam <br/>* 作者 : qianmoQ <br/>* 版本 : 1.0 <br/>* 創(chuàng)建時(shí)間 : 2018-09-23 下午7:40 <br/>* 聯(lián)系作者 : <a href="mailTo:shichengoooo@163.com">qianmoQ</a>*/ @Data @ToString @NoArgsConstructor @AllArgsConstructor public class PageParam {@NotNull(message = "每頁數(shù)據(jù)顯示數(shù)量不能為空")@Min(value = 5)@Max(value = 100)private Integer size; // 每頁數(shù)量@NotNull(message = "當(dāng)前頁顯示數(shù)量不能為空")@Min(value = 1)@Max(value = Integer.MAX_VALUE)private Integer page; // 當(dāng)前頁數(shù)private Boolean flag = true;}
  • @RequestParam調(diào)用方式
@GetMapping(value = "list")public CommonResponseModel findAll(@Validated PageParam param) {...}

這樣的話可以使我們的校驗(yàn)定制化更加簡單。

總結(jié)

以上是生活随笔為你收集整理的Spring校验@RequestParams和@PathVariables参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。