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

如何做qq钓鱼网站办宽带需要多少钱一月

如何做qq钓鱼网站,办宽带需要多少钱一月,做app软件,建设监理工程公司网站自定义异常 ​ 系统中的异常可以分为我们能预知的异常和未知的系统异常,对于我们能预知的异常如空值判断,用户名错误,密码错误等异常我们需要返回客户端,对于系统内部异常如SQL语法错误,参数格式转换错误等需要统一包…

自定义异常

​ 系统中的异常可以分为我们能预知的异常和未知的系统异常,对于我们能预知的异常如空值判断,用户名错误,密码错误等异常我们需要返回客户端,对于系统内部异常如SQL语法错误,参数格式转换错误等需要统一包装成友好的提示后再返回客户端,否则用户也看不懂系统内部的异常。

定义响应码ResponseCode ,方便之后的自定义异常

public enum ResponseCode {RESPONSE_CODE_200(200, "操作成功"),RESPONSE_CODE_400(400, "参数错误"),RESPONSE_CODE_1001(1001, "激活失败已过期"),RESPONSE_CODE_1002(1002, "密码不一致")private Integer code;private String message;ResponseCode(Integer code, String message) {this.code = code;this.message = message;}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}

定义已知异常BusinessException,用于区分项目中的已知异常和未知异常


public class BusinessException extends RuntimeException{private Integer code;public BusinessException(ResponseCode responseCode) {super(responseCode.getMessage());this.code = responseCode.getCode();}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public BusinessException() {super();}public BusinessException(String s) {super(s);}public BusinessException(String message, Throwable cause) {super(message, cause);}public BusinessException(Throwable cause) {super(cause);}protected BusinessException(String message, Throwable cause,boolean enableSuppression,boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);}
}

自定义断言工具类,避免大量if判断

public class AssertUtils {public static void isTrue(Boolean flag, ResponseCode responseCode) {if (!flag) {throw new BusinessException(responseCode);}}public static void isBlank(String str, ResponseCode responseCode) {if (StrUtil.isNotBlank(str)) {throw new BusinessException(responseCode);}}public static void isNotBlank(String str, ResponseCode responseCode) {if (StrUtil.isBlank(str)) {throw new BusinessException(responseCode);}}public static void isNull(Object object, ResponseCode responseCode) {if (Objects.nonNull(object)) {throw new BusinessException(responseCode);}}public static void isNotNull(Object object, ResponseCode responseCode) {if (Objects.isNull(object)) {throw new BusinessException(responseCode);}}public static void isNull(Collection collection, ResponseCode responseCode) {if (collection != null && !collection.isEmpty()) {throw new BusinessException(responseCode);}}public static void isNotNull(Collection collection, ResponseCode responseCode) {if (collection == null || collection.isEmpty()) {throw new BusinessException(responseCode);}}public static void isEq(String str1, String st2, ResponseCode responseCode) {if (!str1.equals(st2)) {throw new BusinessException(responseCode);}}public static void isEqIgnoreCase(String str1, String str2, ResponseCode responseCode) {if (!str1.equalsIgnoreCase(str2)) {throw new BusinessException(responseCode);}}public static void smallerThan(Long second, int i, ResponseCode responseCode) {if (second > i) {throw new BusinessException(responseCode);}}
}

全局异常处理类,不再写大量try - catch,由全局异常处理类自动捕获

@RestControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(BusinessException.class)public AjaxResult businessExceptionHandler(BusinessException e) {e.printStackTrace();return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()).setCode(e.getCode());}//JSR-303校验所抛出的异常@ExceptionHandler(MethodArgumentNotValidException.class)public AjaxResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {e.printStackTrace();BindingResult bindingResult = e.getBindingResult();List<ObjectError> allErrors = bindingResult.getAllErrors();StringBuffer sb = new StringBuffer();allErrors.forEach(objectError -> sb.append(objectError.getDefaultMessage()).append("! "));return AjaxResult.me().setSuccess(false).setMessage(sb.toString()).setCode(ResponseCode.RESPONSE_CODE_400.getCode());}@ExceptionHandler(Exception.class)public AjaxResult ExceptionHandler(Exception e) {e.printStackTrace();return AjaxResult.me().setSuccess(false).setMessage(ResponseCode.RESPONSE_CODE_500.getMessage()).setCode(ResponseCode.RESPONSE_CODE_500.getCode());}
}

在使用dto接受前端参数时,可以使用JSR-303校验

@Data
public class PlaceOrderDTO {private String parentOrderNo;@NotNull(message = "请选择收货地址") // 当为空时会报错 -> "请选择收货地址"private OrderGiftAddress address;}

可以将异常信息定义在properties中在resources包下ValidationMessages.properties配置文件中,将中文转换为Unicode转义序列的UTF-16编码格式

example.error.blank     = \u4e0d\u80fd\u4e3a\u7a7a
\u4e0d 表示中文字符“不”。
\u80fd 表示中文字符“能”。
\u4e3a 表示中文字符“为”。
\u7a7a 表示中文字符“空”。

ValidationMessages.properties配置文件原本是在org.hibernate.validator包下的,因为javaapi中只定义了jsr303规范,具体实现是由其他包实现的,springboot-starter下是由org.hibernate.validator来实现的

@NotBlank(message = "${example.error.blank}")private String username;

在controller接口参数位置打上@Valid,JSR303才能生效

 @PostMapping("/placeorder")public AjaxResult placeOrder(@Valid @RequestBody PlaceOrderDTO dto) {orderGiftService.placeOrder(dto);return AjaxResult.me().setResultObj(dto.getUniPayOrderSn());}```
http://www.yayakq.cn/news/824575/

相关文章:

  • 什么是sem云南网站优化哪家好
  • 郑州铭功路网站建设怎么做能让网站收录的快
  • 网站的运行与维护太原建高铁站
  • 建立购物网站旅游网站建设电子商务的困惑
  • 杭州网站推广服务wordpress-5.1
  • 东莞网站如何制作成都网站设计推荐
  • 网站建设资金管理办法太原做网站的公司
  • 图书馆网站建设方案网站容易出现的问题吗
  • 大良制作网站手机应用商店软件
  • 杰奇怎么做网站地图秦皇岛手机网站
  • 南宁网站建设优势免费注册邮箱
  • ecetc商务网站建设工程师网站建设华网天下制作作
  • asp个人网站怎么建设做网站用到哪些软件
  • 怎样做网站的子网免费营销课程培训
  • 深圳 做公司网站人员优化是什么意思
  • 方城网站建设自媒体代运营怎么收费
  • 诸暨做网站公司加强
  • 如何做整人网站wordpress 搜索框位置
  • 卖视频会员个人网站怎么做购物网站推广怎么做
  • 大连建设学院网站做网站被骗怎么办
  • 南开天津网站建设多城市分站站群cms
  • 网站怎么黑广告设计与制作专升本可以报考什么专业
  • 浙江建设局网站wordpress博客 免费
  • 惠州seo排名外包对seo的理解
  • ui设计师怎么做简历网站怎么生成域名做网站
  • 网站利于搜索wordpress存储视频教程
  • 做室内设计的网站有哪些哪个网站是动态
  • 新浪短网址链接seo外包上海
  • 免费建设网站抽取佣金小公司建网站 优帮云
  • 武城网站建设公司如何策划网络事件营销