当前位置: 首页 > news >正文

坂田杨美企业网站建设网站调用字体库

坂田杨美企业网站建设,网站调用字体库,建筑设计优秀作品集,南阳网站优化我们在写Rest API接口时候会用到很多的RequestParam和PathVariable进行参数的传递#xff0c;但是在校验的时候#xff0c;不像使用RequestBody那样的直接写在实体类中#xff0c;我们这篇文章讲解一下如何去校验这些参数。 依赖配置 要使用Java Validation API#xff0c;… 我们在写Rest API接口时候会用到很多的RequestParam和PathVariable进行参数的传递但是在校验的时候不像使用RequestBody那样的直接写在实体类中我们这篇文章讲解一下如何去校验这些参数。 依赖配置 要使用Java Validation API我们必须添加validation-api依赖项dependencygroupIdjavax.validation/groupIdartifactIdvalidation-api/artifactIdversion2.0.1.Final/version /dependency 通过添加Validated注解来启用控制器中的RequestParams和PathVariables的验证RestController RequestMapping(/) Validated public class Controller {// ... } 校验RequestParam 我们将数字作为请求参数传递给控制器方法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) {// ... } 任何与这些条件不匹配的请求都将返回HTTP状态500并显示默认错误消息。 如果我们尝试调用http://localhost:8080/name-for-day?dayOfWeek24这将返回以下响应信息 There was an unexpected error (typeInternal Server Error, status500). getNameOfDayByNumber.dayOfWeek: must be less than or equal to 7 当然我们也可以在Min和Max注解后面加上message参数进行修改默认的返回信息。 校验PathVariable 和校验RequestParam一样我们可以使用javax.validation.constraints包中的注解来验证PathVariable。 验证String参数不是空且长度小于或等于10GetMapping(/valid-name/{name}) public void test(PathVariable(name) NotBlank Size(max 10) String username) {// ... } 任何名称参数超过10个字符的请求都会导致以下错误消息There was an unexpected error (typeInternal Server Error, status500). createUser.name:size must be between 0 and 10 通过在Size注解中设置message参数可以覆盖默认消息。 其实我们可以看到校验RequestParam和PathVariable参数和我们校验RequestBody方式一致只不过一个是写在了实体中一个写在了外部当然我们也可以将RequestParam的参数写入到实体类中进行使用RequestParam注解进行引入比如我们使用一个分页的实例 分页实体类/*** 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/* 创建时间 : 2018-09-23 下午7:40 br/* 联系作者 : a hrefmailTo:shichengoooo163.comqianmoQ/a*/ Data ToString NoArgsConstructor AllArgsConstructor public class PageParam {NotNull(message 每页数据显示数量不能为空)Min(value 5)Max(value 100)private Integer size; // 每页数量NotNull(message 当前页显示数量不能为空)Min(value 1)Max(value Integer.MAX_VALUE)private Integer page; // 当前页数private Boolean flag true;} RequestParam调用方式 GetMapping(value list)public CommonResponseModel findAll(Validated PageParam param) {...} 这样的话可以使我们的校验定制化更加简单。
http://wiki.neutronadmin.com/news/124044/

相关文章:

  • 杭州高端网站建设商务网站业务流程
  • 零壹网站建设如何选择一个优质网站建设公司
  • 企业网站缺点有没有wordpress上的论坛
  • 网站后台风格校园品牌推广方案
  • 开发公司绩效考核评分细则seo推广手段
  • 天津网站建设方案做体育赛事网站公司
  • 青海中小企业网站建设网站开发的各个阶段及其完成的任务
  • 深圳网站设计+建设首选wordpress the post
  • 上海松江网站设计公司青岛公司网站建设开发
  • 织梦网站程序下载ps个人网站怎么做
  • 网站营销建设策划案58同城广告推广电话
  • 中国广东手机网站建设wordpress页面半透明
  • 重庆网站运营我做推广找不到我的网站
  • 商城网站建设步骤活动拍摄
  • app建设网站公司简介网站图表怎么做的
  • 新手建站教程视频河北网站开发网站
  • 浙江建设信息港网站查询Wordpress付费置顶
  • 泉州设计网站做ppt兼职的网站有哪些
  • 专业做相册书的网站网站建设方为客户提供使用说明书
  • 网站代理被抓建站之星收费版
  • 深圳网站制作公司兴田德润官方网站上海做网站最专业
  • 企业的外币收入外管局网站做啥免认证域名
  • 电商主图设计网站电商运营新手要懂哪些
  • 描写做网站专业的句子优秀网格员推荐表范文
  • 网站代维护做的比较好的医院网站
  • 如何查询网站的域名注册企业培训机构
  • 豆瓣网站模板wordpress 引号被转义
  • 阳春网站开发2022年互联网创业项目
  • 深圳做h5网站做网站环境配置遇到的问题
  • 商城网站都有什么功能北京国际建设集团网站