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

网站建设时 网站信息可以边建设边组织简单网页制作模板

网站建设时 网站信息可以边建设边组织,简单网页制作模板,短期网站开发培训,专业网站建设模板应用中的异常#xff0c;有两件事要考虑#xff0c;怎么处理这个异常#xff0c;怎么把异常可读性高地返回给前端用户 1.怎么把异常可读性高的返回给前端用户或API的消费者 自定义错误代码和错误内容 2.怎么处理异常 比如遇到某个异常时需要发邮件通知IT团队 Controlle…应用中的异常有两件事要考虑怎么处理这个异常怎么把异常可读性高地返回给前端用户 1.怎么把异常可读性高的返回给前端用户或API的消费者 自定义错误代码和错误内容 2.怎么处理异常 比如遇到某个异常时需要发邮件通知IT团队 ControllerAdvice 是 Spring Framework 提供的一个注解用于定义全局的异常处理、数据绑定和模型属性的增强。 它的主要作用是集中管理控制器的全局逻辑例如异常处理而不需要在每个控制器中重复编写相同的代码。   主要功能 全局异常处理通过 ExceptionHandler 注解 捕获并处理控制器中抛出的异常。 全局数据绑定通过 InitBinder 注解对请求参数进行预处理。 全局模型属性通过 ModelAttribute 注解为所有控制器提供公共的模型数据 3.自定义错误代码和内容并且使用ControllerAdvice统一处理异常 3.1 自定义错误码可以把Demo换成projectname public enum ExceptionCode {IN_VALID_REQUEST(Demo0001, ),BAD_REQUEST(Demo400, Bad Request),NOT_FOUND(Demo404, Resource Not Found),INTERNAL_SERVER_ERROR(Demo500, Internal Server Error);private final String code;private final String message;ExceptionCode(String code, String message) {this.code code;this.message message;}public String getCode() {return code;}public String getMessage() {return message;} } 3.2 自定义异常InvalidationException public class InvalidationRequestException extends RuntimeException {public InvalidationRequestException(String message) {super(message);}public InvalidationRequestException(String message, Throwable cause) {super(message, cause);} } 3.3 自定义ExceptionAdvice统一处理exception 当controller中抛出InvalidationRequestException时就会被ControllerAdvice 中的 ExceptionHandler handleInvalidationException()方法捕获并处理该异常 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap; import java.util.Map;ControllerAdvice public class GlobalExceptionHandler {private static final Logger logger LoggerFactory.getLogger(GlobalExceptionHandler.class);ExceptionHandler(InvalidationRequestException.class)ResponseBodypublic ResponseEntityMapString, String handleInvalidationException(InvalidationRequestException ex) {logger.warn(InvalidationRequestException: {}, ex.getMessage());//其它异常处理逻辑比如发邮件打电话通知IT团队//返回给UI或API消费者MapString, String response new HashMap();response.put(code, ExceptionCode.IN_VALIDATED_REQUEST.getCode());response.put(detail,ex.getMessage());return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);}ExceptionHandler(Exception.class)public ResponseEntityString handleGenericException(Exception ex) {return new ResponseEntity(An error occurred: ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);} } 3.4 controller 抛出InvalidationException异常 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;RestController public class DemoController {GetMapping(/validate)public String validateParameter(RequestParam String input) {if (input null || input.trim().isEmpty()) {throw new InvalidationRequestException(Input parameter is invalid or empty);}return Input is valid: input;} } 3.5 测试 启动应用访问 http://localhost:8082/validate?input 校验失败时返回http状态码是400body如下 {code: Demo0001,detail: Input parameter is invalid or empty} 1.异常直接抛出 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;RestController public class DemoController {GetMapping(/throwException)public String throwException() {throw new RuntimeException(This is a runtime exception!);} } 启动应用访问 http://localhost:8082/throwException 会获得一个状态码为500的异常 An error occurred: This is a runtime exception! 思考这个异常对用户或者call API的消费者来说可读性太低不能明确的知道是什么错误 2.自定义状态码和错误信息返回给前端用户或者API 消费者可读性高的异常 自定义错误码可以把Demo换成projectname public enum ExceptionCode {IN_VALIDATED_REQUEST(Demo0001, ),BAD_REQUEST(Demo400, Bad Request),NOT_FOUND(Demo404, Resource Not Found),INTERNAL_SERVER_ERROR(Demo500, Internal Server Error);private final String code;private final String message;ExceptionCode(String code, String message) {this.code code;this.message message;}public String getCode() {return code;}public String getMessage() {return message;} } controller抛出异常 import com.example.demo_async.exception.ExceptionCode; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import java.util.HashMap; import java.util.Map;RestController public class DemoController {GetMapping(/testErrorCode)public ResponseEntityMapString, String testErrorCode() {MapString, String response new HashMap();response.put(code, ExceptionCode.BAD_REQUEST.getCode());response.put(detail, ExceptionCode.BAD_REQUEST.getMessage());return new ResponseEntity(response, HttpStatus.BAD_REQUEST);} } 返回http status  400 body 如下 {code: Demo400,detail: Bad Request} 思考如果有很多个controller方法都会抛出这个异常就需要在每个方法里写一遍异常处理的逻辑会产生大量的重复代码怎么解决呢
http://www.hkea.cn/news/14493304/

相关文章:

  • 网站安全建设方案需求分析ps网站导航条素材
  • 网站建设 需求模板哪些方法可以建设网站
  • 高碑店网站建设价格苏州网站优化哪家好
  • 邢台网站制作公司哪家专业格力网站建设需求分析
  • 网站网页基本情况 网页栏目设置成都装修公司哪家口碑最好
  • 广东网络营销服务网站建设优化推广排名
  • 做网站英文怎么写建行门户网站
  • 专门为98k做的网站php原生态开发公司网站
  • 绿色国外网站优秀企业网站有哪些
  • 西昌有没有可以做网站的公司新闻最新北京消息今天
  • 如何用word做网站做网站的销售好做吗
  • 求个网站带图片素材湛江市建设局网站
  • 竞价单页网站制作婚恋网站做期货现货贵金属的人
  • 苏州自助建站asp在网站开发中的作用
  • 南油网站建设竞价什么意思
  • 百度aipage智能建站网站权重批量查询
  • 查看wordpress密码破解泉州网站seo公司
  • 学校响应式网站模板软件技术是干什么的
  • 做直播网站软件有哪些软件有哪些wordpress 緩慢
  • 互联网站外推广wordpress快速登录插件
  • 中国建设银行投诉网站企业做网站分一般为哪几种类型
  • 綦江网站建设公司上海装修公司口碑哪家好
  • 手机端网站开发视频教程20个排版漂亮的网页设计
  • 迎中国建设银行网站建设银行官方投诉网站
  • 金泉网做网站多少钱qq推广文案怎么写
  • 上海网站开发外包公司深圳各区房价一览表
  • 栾川网站开发怎么做好企业网
  • 萧山做网站哪里找做那个免费观看视频网站
  • 做响应式网站价格石油网页设计与网站建设
  • 一般在什么网站上做电子请帖app推广软件