package com.bolt.gateway.handler; import com.bolt.gateway.provider.ResponseProvider; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler; import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.cloud.gateway.support.NotFoundException; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.server.*; import org.springframework.web.server.ResponseStatusException; import java.util.Map; /** * 异常处理 * * @author arch_group */ public class ErrorExceptionHandler extends DefaultErrorWebExceptionHandler { public ErrorExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) { super(errorAttributes, resourceProperties, errorProperties, applicationContext); } /** * 获取异常属性 */ @Override protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) { int code = 500; Throwable error = super.getError(request); if (error instanceof NotFoundException) { code = 404; } if (error instanceof ResponseStatusException) { code = ((ResponseStatusException) error).getStatus().value(); } return ResponseProvider.response(code, this.buildMessage(request, error)); } /** * 指定响应处理方法为JSON处理的方法 * * @param errorAttributes */ @Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); } /** * 根据code获取对应的HttpStatus * * @param errorAttributes */ @Override protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) { int statusCode = (int) errorAttributes.get("code"); return HttpStatus.valueOf(statusCode); } /** * 构建异常信息 * * @param request * @param ex * @return */ private String buildMessage(ServerRequest request, Throwable ex) { StringBuilder message = new StringBuilder("Failed to handle request ["); message.append(request.methodName()); message.append(" "); message.append(request.uri()); message.append("]"); if (ex != null) { message.append(": "); message.append(ex.getMessage()); } return message.toString(); } }
时间: 2024-04-14 10:31:13 浏览: 33
这是一个自定义的异常处理类,用于处理Spring Cloud Gateway中的异常情况。这个类继承自`DefaultErrorWebExceptionHandler`,并重写了一些方法来自定义异常处理的逻辑。
在这个类中,主要做了以下几件事情:
1. 重写`getErrorAttributes`方法:根据不同的异常类型,设置不同的响应状态码和错误信息。如果是`NotFoundException`异常,设置状态码为404;如果是`ResponseStatusException`异常,获取其对应的HTTP状态码。
2. 重写`getRoutingFunction`方法:指定响应处理方法为JSON处理的方法。
3. 重写`getHttpStatus`方法:根据错误属性中的`code`获取对应的HTTP状态码。
4. 定义了一个私有方法`buildMessage`:用于构建异常信息,包括请求方法、URI和异常消息。
通过重写这些方法,我们可以自定义异常处理的逻辑,包括设置响应状态码和错误信息。如果你还有其他问题,请继续提问。
相关问题
bolt-1.4.6.jar
bolt-1.4.6.jar是一个Java库文件,用于开发和实现分布式消息队列、RPC(远程过程调用)和流式计算等框架。该库文件是基于阿里巴巴开源的Dubbo框架构建的,具有高性能、高可靠性和可扩展性的特点。
bolt-1.4.6.jar为开发人员提供了一套完整的API,使他们能够轻松创建分布式应用程序。该库文件简化了开发过程,提供了自动化的序列化和网络传输机制,使得开发人员可以专注于业务逻辑的实现,而无需关注底层细节。
bolt-1.4.6.jar支持多种通信协议,包括TCP、HTTP、Hessian等,开发人员可以根据具体需求选择合适的通信协议。此外,该库文件还支持多种序列化方式,如Java原生序列化、JSON序列化、FastJSON序列化等,开发人员可以根据需求选择最适合的序列化方式。
bolt-1.4.6.jar还提供了丰富的监控和管理功能,开发人员可以通过监控中心查看系统的运行状态、性能指标和错误信息。此外,该库文件还支持故障转移、负载均衡和集群部署等功能,提高了系统的可靠性和可扩展性。
总之,bolt-1.4.6.jar是一个功能强大、易于使用的Java库文件,为开发人员提供了一套完整的分布式应用程序开发框架,并提供了丰富的功能和扩展性,使开发人员可以更快速、高效地构建分布式系统。
const double Cons_bolt(1.380651e-23); 什么意思
这行代码定义了一个名为 `Cons_bolt` 的常量,它的类型是 `double`,并赋予了一个值为 `1.380651e-23` 的初始化值。这个常量的命名看起来可能是与玻尔兹曼常数(Boltzmann constant)相关的。
玻尔兹曼常数是物理学中的一个重要常数,通常用来描述粒子的热运动和温度之间的关系。其数值约为 `1.380651e-23 J/K`,其中 `J` 表示焦耳,`K` 表示开尔文(温度单位)。在给定的代码中,`Cons_bolt` 被定义为该常数的一个别名,以便在后续的代码中使用这个常量来表示玻尔兹曼常数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)