119 lines
4.2 KiB
Java
119 lines
4.2 KiB
Java
|
|
package com.cdzy.gateway;
|
||
|
|
|
||
|
|
import com.cdzy.common.enums.Code;
|
||
|
|
import com.cdzy.common.model.response.JsonResult;
|
||
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
|
import org.jetbrains.annotations.NotNull;
|
||
|
|
import org.slf4j.Logger;
|
||
|
|
import org.slf4j.LoggerFactory;
|
||
|
|
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
|
||
|
|
import org.springframework.cloud.gateway.support.NotFoundException;
|
||
|
|
import org.springframework.context.annotation.Configuration;
|
||
|
|
import org.springframework.core.annotation.Order;
|
||
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
||
|
|
import org.springframework.http.HttpHeaders;
|
||
|
|
import org.springframework.http.HttpStatus;
|
||
|
|
import org.springframework.http.MediaType;
|
||
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||
|
|
import org.springframework.web.server.ResponseStatusException;
|
||
|
|
import org.springframework.web.server.ServerWebExchange;
|
||
|
|
import reactor.core.publisher.Mono;
|
||
|
|
|
||
|
|
import java.util.Objects;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 网关统一异常处理
|
||
|
|
*
|
||
|
|
* @author ruoyi
|
||
|
|
*/
|
||
|
|
@Order(-1)
|
||
|
|
@Configuration
|
||
|
|
public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
|
||
|
|
private static final Logger log = LoggerFactory.getLogger(GatewayExceptionHandler.class);
|
||
|
|
|
||
|
|
@NotNull
|
||
|
|
@Override
|
||
|
|
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
|
||
|
|
ServerHttpResponse response = exchange.getResponse();
|
||
|
|
|
||
|
|
if (exchange.getResponse().isCommitted()) {
|
||
|
|
return Mono.error(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
String msg;
|
||
|
|
|
||
|
|
if (ex instanceof NotFoundException) {
|
||
|
|
msg = "服务未找到";
|
||
|
|
} else if (ex instanceof ResponseStatusException responseStatusException) {
|
||
|
|
msg = responseStatusException.getMessage();
|
||
|
|
}else {
|
||
|
|
msg = "内部服务器错误";
|
||
|
|
}
|
||
|
|
|
||
|
|
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
|
||
|
|
|
||
|
|
return webFluxResponseWriter(response, msg,null);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置webflux模型响应
|
||
|
|
*
|
||
|
|
* @param response ServerHttpResponse
|
||
|
|
* @param value 响应内容
|
||
|
|
* @param code 响应码
|
||
|
|
* @return Mono<Void>
|
||
|
|
*/
|
||
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value,Integer code) {
|
||
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, Objects.requireNonNullElse(code, Code.FAILED));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置webflux模型响应
|
||
|
|
*
|
||
|
|
* @param response ServerHttpResponse
|
||
|
|
* @param code 响应状态码
|
||
|
|
* @param value 响应内容
|
||
|
|
* @return Mono<Void>
|
||
|
|
*/
|
||
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code) {
|
||
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置webflux模型响应
|
||
|
|
*
|
||
|
|
* @param response ServerHttpResponse
|
||
|
|
* @param status http状态码
|
||
|
|
* @param code 响应状态码
|
||
|
|
* @param value 响应内容
|
||
|
|
* @return Mono<Void>
|
||
|
|
*/
|
||
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code) {
|
||
|
|
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置webflux模型响应
|
||
|
|
*
|
||
|
|
* @param response ServerHttpResponse
|
||
|
|
* @param contentType content-type
|
||
|
|
* @param status http状态码
|
||
|
|
* @param code 响应状态码
|
||
|
|
* @param value 响应内容
|
||
|
|
* @return Mono<Void>
|
||
|
|
*/
|
||
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) {
|
||
|
|
response.setStatusCode(status);
|
||
|
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
||
|
|
JsonResult<String> result = JsonResult.failed(value.toString());
|
||
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
||
|
|
DataBuffer dataBuffer = null;
|
||
|
|
try {
|
||
|
|
dataBuffer = response.bufferFactory().wrap(objectMapper.writeValueAsBytes(result));
|
||
|
|
} catch (JsonProcessingException e) {
|
||
|
|
throw new RuntimeException(e);
|
||
|
|
}
|
||
|
|
return response.writeWith(Mono.just(dataBuffer));
|
||
|
|
}
|
||
|
|
}
|