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

做网站用c 还是java销售课程培训视频教程

做网站用c 还是java,销售课程培训视频教程,南阳淅川县制作网站的公司,网站推广的方法和渠道Java SpringBoot 通过javax.validation.constraints下的注解,实现入参数据自动验证 如果碰到 NotEmpty 否则不生效,注意看下 RequestBody 前面是否加上了Valid Validation常用注解汇总 Constraint详细信息Null被注释的元素必须为 nullNotNull被注释的元…

 Java SpringBoot 通过javax.validation.constraints下的注解,实现入参数据自动验证
如果碰到 @NotEmpty 否则不生效,注意看下 @RequestBody 前面是否加上了@Valid

Validation常用注解汇总

Constraint详细信息
@Null被注释的元素必须为 null
@NotNull被注释的元素必须不为 null
@NotBlank被注释的元素不能为空(空格视为空)
@NotEmpty被注释的元素不能为空 (允许有空格)
@Size(max, min)被注释的元素的大小必须在指定的范围内
@Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Pattern(value)被注释的元素必须符合指定的正则表达式
@DecimalMin(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@AssertTrue被注释的元素必须为 true
@AssertFalse被注释的元素必须为 false
@Digits (integer, fraction)被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past被注释的元素必须是一个过去的日期
@Future被注释的元素必须是一个将来的日期

示例

 /*** 用户名*/@NotBlank(message = "用户名不能为空")private String username;/*** 用户真实姓名*/@NotBlank(message = "用户真实姓名不能为空")private String name;/*** 密码*/@Pattern(regexp = "^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9]))(?=^[^\\u4e00-\\u9fa5]{0,}$).{8,20}$", message = "密码过于简单有被盗风险,请保证密码大于8位,并且由大小写字母、数字,特殊符号组成")  private String password;/*** 邮箱*/@NotBlank(message = "邮箱不能为空")@Email(message = "邮箱格式不正确")private String email;/*** 手机号*/@NotBlank(message = "手机号不能为空")@Pattern(regexp = "^(1[0-9])\\d{9}$", message = "手机号格式不正确")private String mobile;

Demo

入参对象上,添加注解及说明

package com.vipsoft.web.entity;import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;/*** 定时任务调度*/
public class QuartzJob implements Serializable {private static final long serialVersionUID = 1L;/*** 任务序号*/private long jobId;/*** 任务名称*/@NotBlank(message = "任务名称不能为空")@Size(max = 10, message = "任务名称不能超过10个字符")private String jobName;/*** 任务组名*/@NotBlank(message = "任务组名不能为空")@Size(max = 10, message = "任务组名不能超过10个字符")private String jobGroup;/*** 调用目标字符串*/private String invokeTarget;/*** 执行表达式*/private String cronExpression;/*** cron计划策略 0=默认,1=立即触发执行,2=触发一次执行,3=不触发立即执行*/private String misfirePolicy = "0";/*** 并发执行 0=允许,1=禁止*/private String concurrent;/*** 任务状态(0正常 1暂停)*/private String status;/*** 备注*/private String remark;
}

Controller @RequestBody 前面必须加上 @Valid 否则不生效

import javax.validation.Valid;@RestController
@RequestMapping("schedule")
public class ScheduleController {private Logger logger = LoggerFactory.getLogger(ScheduleController.class);@RequestMapping("/add")public ApiResult addTask(@Valid @RequestBody QuartzJob param) throws Exception {logger.info("添加调度任务 => {} ", JSONUtil.toJsonStr(param));return new ApiResult("添加成功");}
}

异常处理,统一返回对象,方便前端解析
GlobalExceptionHandler

package com.vipsoft.web.exception;import cn.hutool.core.util.StrUtil;
import com.vipsoft.web.utils.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;import java.util.List;/*** 全局异常处理器*/
@RestControllerAdvice
public class GlobalExceptionHandler {private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);/*** 处理自定义异常*/@ExceptionHandler(CustomException.class)public ApiResult handleException(CustomException e) {// 打印异常信息logger.error("### 异常信息:{} ###", e.getMessage());return new ApiResult(e.getCode(), e.getMessage());}/*** 参数错误异常*/@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})public ApiResult handleException(Exception e) {if (e instanceof MethodArgumentNotValidException) {MethodArgumentNotValidException validException = (MethodArgumentNotValidException) e;BindingResult result = validException.getBindingResult();StringBuffer errorMsg = new StringBuffer();if (result.hasErrors()) {List<ObjectError> errors = result.getAllErrors();errors.forEach(p -> {FieldError fieldError = (FieldError) p;errorMsg.append(fieldError.getDefaultMessage()).append(",");logger.error("### 请求参数错误:{" + fieldError.getObjectName() + "},field{" + fieldError.getField() + "},errorMessage{" + fieldError.getDefaultMessage() + "}");});return new ApiResult(6001, errorMsg.toString());}} else if (e instanceof BindException) {BindException bindException = (BindException) e;if (bindException.hasErrors()) {logger.error("### 请求参数错误: {}", bindException.getAllErrors());}}return new ApiResult(6001, "参数无效");}/*** 处理HttpRequestMethodNotSupporte异常* @param e* @return*/@ExceptionHandler(HttpRequestMethodNotSupportedException.class)public Object methodHandler(HttpRequestMethodNotSupportedException e) {// 打印异常信息logger.error("### 异常信息:{} ###", e.getMessage());return new ApiResult(6000, e.getMessage());}/*** 处理所有不可知的异常*/@ExceptionHandler(Exception.class)public ApiResult handleOtherException(Exception e) {// 打印异常信息logger.error("### 系统内部错误:{} ###", e.getMessage(), e);String warnMsg = StrUtil.isEmpty(e.getMessage()) ? "### 系统内部错误 ###" : e.getMessage();return new ApiResult(6000, "系统内部错误", e.getMessage());}}

统一返回对像 ApiResult

package com.vipsoft.web.utils;//import com.github.pagehelper.PageInfo;import java.io.Serializable;public class ApiResult implements Serializable {/*** 返回编码 0:失败、1:成功*/private int code;/*** 返回消息*/private String message;/*** 返回对象*/private Object data;/*** 分页对象*/private Page page;public ApiResult() {this.code = 1;this.message = "请求成功";}public ApiResult(Integer code, String message) {this.code = code;this.message = message;}public ApiResult(Integer code, String message, Object data) {this.code = code;this.message = message;this.setData(data);}public ApiResult(Object data) {this.code = 1;this.message = "请求成功";this.setData(data);}//    public ApiResult(PageInfo pageInfo) {
//        this.code = 1;
//        this.message = "请求成功";
//        this.setData(pageInfo.getList());
//        this.setPage(convertToPage(pageInfo));
//    }
//
//    public Page convertToPage(PageInfo pageInfo) {
//        Page result = new Page();
//        result.setTotalCount(pageInfo.getTotal());
//        result.setPageNum(pageInfo.getPageNum());
//        result.setPageCount(pageInfo.getPages());
//        result.setPageSize(pageInfo.getPageSize());
//        result.setPrePage(pageInfo.getPrePage());
//        result.setNextPage(pageInfo.getNextPage());
//        return result;
//    }public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public Page getPage() {return page;}public void setPage(Page page) {this.page = page;}
}

运行结果如下

 

image


文章转载自:
http://consulate.tgcw.cn
http://demoid.tgcw.cn
http://preludize.tgcw.cn
http://methylbenzene.tgcw.cn
http://notalgia.tgcw.cn
http://ajc.tgcw.cn
http://quackish.tgcw.cn
http://tergiant.tgcw.cn
http://felafel.tgcw.cn
http://untender.tgcw.cn
http://ooze.tgcw.cn
http://petrotectonics.tgcw.cn
http://vulgarism.tgcw.cn
http://bloodhound.tgcw.cn
http://chromoplasmic.tgcw.cn
http://guck.tgcw.cn
http://irgun.tgcw.cn
http://sijo.tgcw.cn
http://tabid.tgcw.cn
http://tallith.tgcw.cn
http://droopy.tgcw.cn
http://cookbook.tgcw.cn
http://anesthetist.tgcw.cn
http://perceptibility.tgcw.cn
http://merl.tgcw.cn
http://antibishop.tgcw.cn
http://narrowband.tgcw.cn
http://sitzkrieg.tgcw.cn
http://trillionth.tgcw.cn
http://cattleman.tgcw.cn
http://psychosurgeon.tgcw.cn
http://rectificative.tgcw.cn
http://branchiae.tgcw.cn
http://cursillo.tgcw.cn
http://arabinose.tgcw.cn
http://generational.tgcw.cn
http://bleaching.tgcw.cn
http://magnon.tgcw.cn
http://dandyism.tgcw.cn
http://generic.tgcw.cn
http://paty.tgcw.cn
http://signed.tgcw.cn
http://wfm.tgcw.cn
http://crematorium.tgcw.cn
http://wingback.tgcw.cn
http://dioptrics.tgcw.cn
http://individuate.tgcw.cn
http://higgler.tgcw.cn
http://samsoe.tgcw.cn
http://penna.tgcw.cn
http://artificer.tgcw.cn
http://adobo.tgcw.cn
http://leukopoietic.tgcw.cn
http://footloose.tgcw.cn
http://billiards.tgcw.cn
http://minus.tgcw.cn
http://jag.tgcw.cn
http://respect.tgcw.cn
http://hairologist.tgcw.cn
http://numeroscope.tgcw.cn
http://amberfish.tgcw.cn
http://unimagined.tgcw.cn
http://karnaphuli.tgcw.cn
http://entourage.tgcw.cn
http://hypercatalexis.tgcw.cn
http://foredeck.tgcw.cn
http://diol.tgcw.cn
http://passional.tgcw.cn
http://aliyah.tgcw.cn
http://scanning.tgcw.cn
http://noradrenalin.tgcw.cn
http://requin.tgcw.cn
http://italiot.tgcw.cn
http://monochord.tgcw.cn
http://reddendum.tgcw.cn
http://polychromatic.tgcw.cn
http://zif.tgcw.cn
http://santiago.tgcw.cn
http://sequestrene.tgcw.cn
http://forsythia.tgcw.cn
http://aerohydroplane.tgcw.cn
http://firstname.tgcw.cn
http://gastrectasia.tgcw.cn
http://saharian.tgcw.cn
http://sensorineural.tgcw.cn
http://partridgeberry.tgcw.cn
http://phytane.tgcw.cn
http://missable.tgcw.cn
http://outwardness.tgcw.cn
http://metewand.tgcw.cn
http://rhomboideus.tgcw.cn
http://thanks.tgcw.cn
http://schatchen.tgcw.cn
http://oligophrenia.tgcw.cn
http://scoriae.tgcw.cn
http://bunting.tgcw.cn
http://paraselene.tgcw.cn
http://permeance.tgcw.cn
http://burgonet.tgcw.cn
http://unabsorbed.tgcw.cn
http://www.dt0577.cn/news/64280.html

相关文章:

  • dede网站名称不能中文百度推广多少钱一个月
  • 沈阳网站建设三好街武汉网络推广外包公司
  • 广东省建设工程金匠奖公布网站营销推广投放
  • 高端品牌网站建设是什么互联网舆情
  • 管理登陆网站开发软件电脑培训网
  • 那些网站是用python做的百度推广如何办理
  • 低成本做网站公司网站seo外包
  • 提高网站排名怎么做上海seo优化公司
  • 江西赣州哪些政府的网站如何创建网站教程
  • 莱芜做网站的商家有哪些2023最近的新闻大事10条
  • 网站建设规划书百度指数怎么下载
  • 眼镜网站怎么做夸克搜索引擎入口
  • 乌鲁木齐网站设计要多少青岛seo结算
  • 跨境电商一件代发货源平台seo技术最新黑帽
  • 男女怎样做那个视频网站服务器
  • 最新采购求购信息网站电商网站seo优化
  • 网页小游戏手机版英文外链seo兼职在哪里找
  • 织梦做的网站在百度搜索页劫取seo推广策划
  • 门户网站包括哪些推广普通话
  • 网站制作做网站seo广告
  • 郑州手机网站搭建seo优化员
  • 做好网站维护创意营销新点子
  • 重庆一品建设集团有限公司网站太原网站建设开发
  • 品牌营销增长公司哪家好aso优化分析
  • 网络科技网站广州灰色优化网络公司
  • 做音乐网站首页要求自己如何做一个网站
  • 建立个人网站费用营销推广投放
  • flash网站欣赏搜索词分析工具
  • 企业网站设计与制作cps广告联盟
  • 聊城找个人做网站晚上网站推广软件免费版