前言
Gateway網(wǎng)關(guān)統(tǒng)一全局異常處理操作 方便前端看到 這里要精細化翻譯,默認返回用戶是看不懂的 所以需要配置一個 Gateway 的全局異常處理器
如果沒有網(wǎng)關(guān)全局異常的 會如下截圖
一、使用步驟
1.編寫 GlobalExceptionHandler
代碼如下:
package cn.cws.framework.gatewayservice.handler;
import cn.cws.framework.core.common.service.ApiResult;
import cn.cws.framework.gatewayservice.handler.code.GatewayErrorCode;
import cn.cws.framework.gatewayservice.util.WebFrameworkUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* Gateway 的全局異常處理器,將 Exception 翻譯成 ApiResult + 對應的異常編號
*
* 在功能上,和 核心 的 GlobalExceptionHandler 類是一致的
*
* @author zw
*/
@Component
@Order(-1) // 保證優(yōu)先級高于默認的 Spring Cloud Gateway 的 ErrorWebExceptionHandler 實現(xiàn)
@Slf4j
public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
// 已經(jīng) commit,則直接返回異常
ServerHttpResponse response = exchange.getResponse();
if (response.isCommitted()) {
return Mono.error(ex);
}
// 轉(zhuǎn)換成 CommonResult
ApiResult<?> result;
if (ex instanceof ResponseStatusException) {
result = responseStatusExceptionHandler(exchange, (ResponseStatusException) ex);
} else {
result = defaultExceptionHandler(exchange, ex);
}
// 返回給前端
return WebFrameworkUtils.writeJSON(exchange, result);
}
/**
* 處理 Spring Cloud Gateway 默認拋出的 ResponseStatusException 異常
*/
private ApiResult<?> responseStatusExceptionHandler(ServerWebExchange exchange,
ResponseStatusException ex) {
// TODO zw 這里要精細化翻譯,默認返回用戶是看不懂的
ServerHttpRequest request = exchange.getRequest();
log.error("[responseStatusExceptionHandler][uri({}/{}) 發(fā)生異常]", request.getURI(), request.getMethod(), ex);
if (ex.getRawStatusCode()== GatewayErrorCode.SYS_GATEWAY_ERROR_NACOS.getCode()){
return ApiResult.buildFail(ex.getRawStatusCode(), " 請聯(lián)系管理人員 : "+ex.getReason()+GatewayErrorCode
.SYS_GATEWAY_ERROR_NACOS.getMsg());
}
return ApiResult.buildFail(ex.getRawStatusCode(), ex.getReason());
// return ApiResult.buildFail(ex.getRawStatusCode(), ex.getReason());
}
/**
* 處理系統(tǒng)異常,兜底處理所有的一切
*/
@ExceptionHandler(value = Exception.class)
public ApiResult<?> defaultExceptionHandler(ServerWebExchange exchange,
Throwable ex) {
ServerHttpRequest request = exchange.getRequest();
log.error("[defaultExceptionHandler][uri({}/{}) 發(fā)生異常]", request.getURI(), request.getMethod(), ex);
// TODO zw 是否要插入異常日志呢?
// 返回 ERROR CommonResult
log.error("[responseStatusExceptionHandler][uri({}/{}) 發(fā)生異常]", request.getURI(), request.getMethod(), ex);
return ApiResult.buildFail(-1, ex.getMessage());
// return ApiResult.buildFail(-1,ex.getStackTrace().toString());
}
}
2.WebFrameworkUtils 工具類
代碼如下:
package cn.cws.framework.gatewayservice.util;
import cn.cws.framework.core.common.util.JsonKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* Web 工具類
*
*
* @author zw
*/
@Slf4j
public class WebFrameworkUtils {
private static final String HEADER_TENANT_ID = "tenant-id";
private WebFrameworkUtils() {}
/**
* 將 Gateway 請求中的 header,設置到 HttpHeaders 中
*
* @param tenantId 租戶編號
* @param httpHeaders WebClient 的請求
*/
public static void setTenantIdHeader(Long tenantId, HttpHeaders httpHeaders) {
if (tenantId == null) {
return;
}
httpHeaders.set(HEADER_TENANT_ID, String.valueOf(tenantId));
}
public static Long getTenantId(ServerWebExchange exchange) {
String tenantId = exchange.getRequest().getHeaders().getFirst(HEADER_TENANT_ID);
return tenantId != null ? Long.parseLong(tenantId) : null;
}
/**
* 返回 JSON 字符串
*
* @param exchange 響應
* @param object 對象,會序列化成 JSON 字符串
*/
@SuppressWarnings("deprecation") // 必須使用 APPLICATION_JSON_UTF8_VALUE,否則會亂碼
public static Mono<Void> writeJSON(ServerWebExchange exchange, Object object) {
// 設置 header
ServerHttpResponse response = exchange.getResponse();
response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
// 設置 body
return response.writeWith(Mono.fromSupplier(() -> {
DataBufferFactory bufferFactory = response.bufferFactory();
try {
return bufferFactory.wrap(JsonKit.toJsonByte(object));
} catch (Exception ex) {
ServerHttpRequest request = exchange.getRequest();
log.error("[writeJSON][uri({}/{}) 發(fā)生異常]", request.getURI(), request.getMethod(), ex);
return bufferFactory.wrap(new byte[0]);
}
}));
}
/**
* 獲得請求匹配的 Route 路由
*
* @param exchange 請求
* @return 路由
*/
public static Route getGatewayRoute(ServerWebExchange exchange) {
return exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
}
}
3.json 工具類
代碼如下:
package cn.cws.framework.core.common.util;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
/**
* @author zw
*/
public class JsonKit {
public static final ObjectMapper OBJECT_MAPPER = createObjectMapper();
private static final ObjectMapper IGNORE_OBJECT_MAPPER = createIgnoreObjectMapper();
private static ObjectMapper createIgnoreObjectMapper() {
ObjectMapper objectMapper = createObjectMapper();
objectMapper.addMixIn(Object.class, DynamicMixIn.class);
return objectMapper;
}
/**
* 初始化ObjectMapper
*/
public static ObjectMapper createObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}
public static String object2Json(Object o) {
StringWriter sw = new StringWriter();
JsonGenerator gen = null;
try {
gen = new JsonFactory().createGenerator(sw);
OBJECT_MAPPER.writeValue(gen, o);
} catch (IOException e) {
throw new RuntimeException("不能序列化對象為Json", e);
} finally {
if (null != gen) {
try {
gen.close();
} catch (IOException e) {
throw new RuntimeException("不能序列化對象為Json", e);
}
}
}
return sw.toString();
}
@SneakyThrows
public static byte[] toJsonByte(Object object) {
return OBJECT_MAPPER.writeValueAsBytes(object);
}
@SneakyThrows
public static String toJsonString(Object object) {
return OBJECT_MAPPER.writeValueAsString(object);
}
/**
* @param ignoreFiledNames 要忽略的屬性名
*/
public static String object2Json(Object o, String... ignoreFiledNames) {
try {
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept(ignoreFiledNames);
FilterProvider filters = new SimpleFilterProvider().addFilter("dynamicFilter", theFilter);
return IGNORE_OBJECT_MAPPER.writer(filters).writeValueAsString(o);
} catch (IOException e) {
throw new RuntimeException("不能序列化對象為Json", e);
}
}
@JsonFilter("dynamicFilter")
private static class DynamicMixIn {
}
public static Map<String, Object> object2Map(Object o) {
return OBJECT_MAPPER.convertValue(o, Map.class);
}
/**
* 將 json 字段串轉(zhuǎn)換為 對象.
*
* @param json 字符串
* @param clazz 需要轉(zhuǎn)換為的類
*/
public static <T> T json2Object(String json, Class<T> clazz) {
try {
return OBJECT_MAPPER.readValue(json, clazz);
} catch (IOException e) {
throw new RuntimeException("將 Json 轉(zhuǎn)換為對象時異常,數(shù)據(jù)是:" + json, e);
}
}
/**
* 將 json 字段串轉(zhuǎn)換為 List.
*/
public static <T> List<T> json2List(String json, Class<T> clazz) {
JavaType type = OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz);
try {
return OBJECT_MAPPER.readValue(json, type);
} catch (Exception e) {
return null;
}
}
/**
* 將 json 字段串轉(zhuǎn)換為 數(shù)據(jù).
*/
public static <T> T[] json2Array(String json, Class<T[]> clazz) throws IOException {
return OBJECT_MAPPER.readValue(json, clazz);
}
public static <T> T node2Object(JsonNode jsonNode, Class<T> clazz) {
try {
T t = OBJECT_MAPPER.treeToValue(jsonNode, clazz);
return t;
} catch (JsonProcessingException e) {
throw new RuntimeException("將 Json 轉(zhuǎn)換為對象時異常,數(shù)據(jù)是:" + jsonNode.toString(), e);
}
}
public static JsonNode object2Node(Object o) {
try {
if (o == null) {
return OBJECT_MAPPER.createObjectNode();
} else {
return OBJECT_MAPPER.convertValue(o, JsonNode.class);
}
} catch (Exception e) {
throw new RuntimeException("不能序列化對象為Json", e);
}
}
/**
* JsonNode轉(zhuǎn)換為Java泛型對象,可以是各種類型。
*
* @param json String
* @param tr TypeReference,例如: new TypeReference< List<FamousUser> >(){}
* @return List對象列表
*/
public static <T> T json2GenericObject(String json, TypeReference<T> tr) {
if (json == null || "".equals(json)) {
throw new RuntimeException("將 Json 轉(zhuǎn)換為對象時異常,數(shù)據(jù)是:" + json);
} else {
try {
return (T) OBJECT_MAPPER.readValue(json, tr);
} catch (Exception e) {
throw new RuntimeException("將 Json 轉(zhuǎn)換為對象時異常,數(shù)據(jù)是:" + json, e);
}
}
}
}
4. 網(wǎng)關(guān)異常狀態(tài)碼
package cn.cws.framework.gatewayservice.handler.code;
import cn.cws.framework.core.common.constant.ModelEnum;
/**
* @description: 網(wǎng)關(guān)異常狀態(tài)碼
* @author:zw,微信:yingshengzw
* @date: 2023/1/16
* @Copyright: 公眾號:搬磚暗夜碼農(nóng) | 博客:https://itzhouwei.com - 沉淀、分享、成長,讓自己和他人都能有所收獲!
*/
@SuppressWarnings("all")
public enum GatewayErrorCode {
SYS_GATEWAY_ERROR_NACOS(503," 服務沒有注冊到nacos上");
private String msg;
private int code;
GatewayErrorCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
5. 測試結(jié)果如下
文章來源:http://www.zghlxwxcb.cn/news/detail-523487.html
總結(jié)
以上就是今天要講的內(nèi)容,本文僅僅簡單 所以需要配置一個 Gateway 的全局異常處理器文章來源地址http://www.zghlxwxcb.cn/news/detail-523487.html
到了這里,關(guān)于【微服務網(wǎng)關(guān)---Gateway 的全局異常處理器】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!