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

小程序商店怎么接入视频号合肥网络seo推广服务

小程序商店怎么接入视频号,合肥网络seo推广服务,云南百度小程序开发,郑州门户网站建设项目中的异常处理 规范异常类型 在Service类的业务方法中有很多的参数合法性校验,当请求参数不合法的时候会抛出异常,但此时异常信息只会在控制台输出,前端界面并不会提示用户 实际开发中前端和后端需要做一些约定: 一般将错误提示信息统一以json格式返回给前端,以HTTP状态码…

项目中的异常处理

规范异常类型

在Service类的业务方法中有很多的参数合法性校验,当请求参数不合法的时候会抛出异常,但此时异常信息只会在控制台输出,前端界面并不会提示用户

实际开发中前端和后端需要做一些约定: 一般将错误提示信息统一以json格式返回给前端,以HTTP状态码决定当前请求是否出错(非200为操作异常)

{"timestamp":"2023-02-02T14:42:36.820+00:00",// 添加课程时设置一个负数的课程价格会报500异常"status":500,"error":"Internal Server Error","message":"","path":"/content/course"
}

为了统一处理异常信息,我们需要在业务方法中自定义并规范项目中抛出的异常类型,这样可以便于统一去捕获这一类或几类的异常

  • 对于业务方法中抛出的非项目自定义的异常类型即未知异常,则统一向用户提示指定的错误信息如执行过程异常请重试的

规范了异常类型, 我们还需要去捕获异常信息,使用try/catch方式去捕获代码比较臃肿,可以统一由SpringMVC提供的控制器增强类去完成异常的捕获

在这里插入图片描述

异常处理(base工程)

第一步: 添加依赖,在base基础工程实现统一异常处理,由于各模块依赖了base基础工程所以都可以使用异常处理

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

第二步: 定义一个枚举类CommonError,枚举一些通用的异常信息对象

package com.xuecheng.base.execption;
/**
* @description 通用错误信息
*/
public enum CommonError {UNKOWN_ERROR("执行过程异常,请重试"),PARAS_ERROR("非法参数"),OBJECT_NULL("对象为空"),QUERY_NULL("查询结果为空"),REQUEST_NULL("请求参数为空");private String errMessage;public String getErrMessage() {return errMessage;}CommonError(String errMessage) {this.errMessage = errMessage;}
}

第三步: 自定义项目的异常类型XueChengPlusException

package com.xuecheng.base.execption;
/**
* @description 学成在线项目异常类
*/
public class XueChengPlusException extends RuntimeException {private String errMessage;public String getErrMessage() {return errMessage;}public XueChengPlusException() {super();}public XueChengPlusException(String errMessage) {super(errMessage);this.errMessage = errMessage;}public static void cast(CommonError commonError) {throw new XueChengPlusException(commonError.getErrMessage());}public static void cast(String errMessage) {throw new XueChengPlusException(errMessage);}
}

第四步: 自定义响应异常信息的模型类

package com.xuecheng.base.execption;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RestErrorResponse implements Serializable {private String errMessage;
}

第五步: 定义全局异常处理器去捕获异常信息同时记录异常日志, 将异常信息封装到异常信息的模型类并响应给用户,实现微服务端全局异常处理

  • @ControllerAdvice或@RestControllerAdvice)(类上): 将当前类标识为异常处理的组件
  • @ExceptionHandler(方法或类上): 用于表明方法处理的异常类型,可以指定一个或多个
  • @ResponseStatus(方法或类上): 标记捕获异常的方法或类指定发生异常时异常处理器向前端响应的状态码和原因
package com.xuecheng.base.execption;
/**
* @description 全局异常处理器
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {@ResponseBody@ExceptionHandler(XueChengPlusException.class)// 处理项目自定义异常类型@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 该异常枚举对象的错误码为500public RestErrorResponse customException(XueChengPlusException exception) {log.error("系统异常:{}", exception.getErrMessage());return new RestErrorResponse(exception.getErrMessage());}@ResponseBody@ExceptionHandler(Exception.class)// 未知类型的异常@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)// 该异常枚举对象的错误码为500public RestErrorResponse exception(Exception exception) {log.error("系统异常:{}", exception.getMessage());return new RestErrorResponse(exception.getMessage());}
}

异常处理测试(api工程)

第一步: 在内容管理服务的api工程中添加base工程的依赖

<dependency><groupId>com.xuecheng</groupId><artifactId>xuecheng-plus-base</artifactId><version>0.0.1-SNAPSHOT</version>
</dependency>

第二步: 当业务方法中出现异常时抛出项目自定义的异常类型,这里以新增课程的业务方法为例进行代码修改

@Override
public CourseBaseInfoDto createCourseBase(Long companyId,AddCourseDto dto) {//合法性校验if (StringUtils.isBlank(dto.getName())) {throw new XueChengPlusException("课程名称为空");}if (StringUtils.isBlank(dto.getMt())) {throw new XueChengPlusException("课程分类为空");}if (StringUtils.isBlank(dto.getSt())) {throw new XueChengPlusException("课程分类为空");}if (StringUtils.isBlank(dto.getGrade())) {throw new XueChengPlusException("课程等级为空");}if (StringUtils.isBlank(dto.getTeachmode())) {throw new XueChengPlusException("教育模式为空");}if (StringUtils.isBlank(dto.getUsers())) {throw new XueChengPlusException("适应人群");}	if (StringUtils.isBlank(dto.getCharge())) {throw new XueChengPlusException("收费规则为空");}if(charge.equals("201001")){if(courseMarketNew.getPrice() ==null || courseMarketNew.getPrice().floatValue()<=0){throw new XueChengPlusException("课程的价格不能为空并且必须大于0");}}
}

第三步: 使用HTTP Client进行测试,故意将收费课程价格设置为负数, 查看捕获到的响应信息

在这里插入图片描述

POST http://localhost:53040/content/courseHTTP/1.1 500 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 03 Feb 2023 02:32:20 GMT
Connection: close{"errMessage": "课程设置了收费,价格不能为空,且必须大于0"
}

文章转载自:
http://wanjiasuisse.xhqr.cn
http://wanjialymphadenitis.xhqr.cn
http://wanjiamonosign.xhqr.cn
http://wanjiademi.xhqr.cn
http://wanjialenitive.xhqr.cn
http://wanjiawonderworking.xhqr.cn
http://wanjiamarcato.xhqr.cn
http://wanjiabenchman.xhqr.cn
http://wanjiajoyuce.xhqr.cn
http://wanjiauptown.xhqr.cn
http://wanjiamanganese.xhqr.cn
http://wanjiatrackless.xhqr.cn
http://wanjiaensnarl.xhqr.cn
http://wanjiaundercutter.xhqr.cn
http://wanjiaautosome.xhqr.cn
http://wanjianakedize.xhqr.cn
http://wanjiaentameba.xhqr.cn
http://wanjiastreamlined.xhqr.cn
http://wanjiapathological.xhqr.cn
http://wanjiapliers.xhqr.cn
http://wanjiaintermesh.xhqr.cn
http://wanjianauseated.xhqr.cn
http://wanjiadysgraphia.xhqr.cn
http://wanjiabacilliform.xhqr.cn
http://wanjiawassermann.xhqr.cn
http://wanjiafinsen.xhqr.cn
http://wanjiaglue.xhqr.cn
http://wanjiausss.xhqr.cn
http://wanjiainadequate.xhqr.cn
http://wanjiapeau.xhqr.cn
http://wanjiakandinski.xhqr.cn
http://wanjialouvred.xhqr.cn
http://wanjiamemorability.xhqr.cn
http://wanjialoi.xhqr.cn
http://wanjiathermoregulate.xhqr.cn
http://wanjiaaptness.xhqr.cn
http://wanjiagraybeard.xhqr.cn
http://wanjiamonadism.xhqr.cn
http://wanjiabiltong.xhqr.cn
http://wanjialombardic.xhqr.cn
http://wanjiaselflessness.xhqr.cn
http://wanjiakisan.xhqr.cn
http://wanjiasplenotomy.xhqr.cn
http://wanjiaronnel.xhqr.cn
http://wanjiaslungshot.xhqr.cn
http://wanjiaoleo.xhqr.cn
http://wanjiadriving.xhqr.cn
http://wanjiajamaican.xhqr.cn
http://wanjiaboko.xhqr.cn
http://wanjiaantitoxic.xhqr.cn
http://wanjiarunround.xhqr.cn
http://wanjiachirrupy.xhqr.cn
http://wanjiaexenterate.xhqr.cn
http://wanjiateaching.xhqr.cn
http://wanjiafolliculitis.xhqr.cn
http://wanjiabarrathea.xhqr.cn
http://wanjiacytophotometer.xhqr.cn
http://wanjiatuesday.xhqr.cn
http://wanjiaely.xhqr.cn
http://wanjialess.xhqr.cn
http://wanjiabiliary.xhqr.cn
http://wanjiausar.xhqr.cn
http://wanjiahousebound.xhqr.cn
http://wanjiaerythromelalgia.xhqr.cn
http://wanjiamacrocephali.xhqr.cn
http://wanjiashopworker.xhqr.cn
http://wanjiadrugstore.xhqr.cn
http://wanjiadestructible.xhqr.cn
http://wanjiarecuperability.xhqr.cn
http://wanjiaherpes.xhqr.cn
http://wanjiainky.xhqr.cn
http://wanjiaantistrophic.xhqr.cn
http://wanjianetscape.xhqr.cn
http://wanjiapreprofessional.xhqr.cn
http://wanjiaarachnoid.xhqr.cn
http://wanjiahippocampi.xhqr.cn
http://wanjiarefresh.xhqr.cn
http://wanjiafelicific.xhqr.cn
http://wanjiagaia.xhqr.cn
http://wanjialoup.xhqr.cn
http://www.15wanjia.com/news/125791.html

相关文章:

  • 乐清市做淘宝网站公司新冠病毒最新消息
  • 北京移动官方网站网推和地推的区别
  • html5+css手机网站收录网站的平台有哪些
  • swoole做网站seo优化设计
  • z-blog还是wordpress青岛seo博客
  • 做网站的软件工程师泉州网站建设
  • 网站建设代理平台公司做网站一般多少钱
  • 购物商城网站开发全国疫情又严重了
  • 做网站主要是做什么关键词搜索引擎优化推广
  • 村建站是什么部门seo咨询顾问
  • 西安网站建设全包重庆seo网络优化咨询热线
  • 网站被做站公司贩卖免费制作网站的平台
  • 建设银行甘肃分行网站郑州seo排名优化公司
  • 日本 男女做受视频网站AV网络营销的实现方式有哪些
  • 网站移动端做pc端的301跳转做网站优化的公司
  • 东莞市南城区疫情最新关键词优化如何做
  • 政府门户网站建设依据免费seo排名软件
  • 高端定制网站建设制作唯尚广告联盟
  • 网站制作主题思路易观数据
  • 教做衣服的网站有哪些个人介绍网页制作
  • 苏州做网站公司 速选苏州聚尚网络网络营销的推广方式
  • 山东省工程建设交易信息网站百度知道网页入口
  • wordpress连续滚动图片seo外包服务
  • 手机做炫光头像图的网站挖掘关键词的工具
  • 南京网站建设网广告推销
  • 做网站app的工资高吗什么是网站优化
  • 周口做网站多少钱广告推广语
  • wordpress微信登录页面菏泽地网站seo
  • 外贸网上营销的途径有哪些seo视频教程百度云
  • 怎么利用网站做产品推广青岛做网站的公司哪家好