fix #I1P1IL
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.central.gateway.config;
|
||||
|
||||
import feign.codec.Decoder;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
|
||||
import org.springframework.cloud.openfeign.support.SpringDecoder;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Feign配置类
|
||||
* 配置Feign的Decoder解决在Gateway中使用Feign时报错找不到HttpMessageConverters
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2020/7/26
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
public class FeignConfig {
|
||||
@Bean
|
||||
public Decoder feignDecoder() {
|
||||
return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
|
||||
}
|
||||
|
||||
public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
|
||||
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new GateWayMappingJackson2HttpMessageConverter());
|
||||
return () -> httpMessageConverters;
|
||||
}
|
||||
|
||||
public static class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
|
||||
GateWayMappingJackson2HttpMessageConverter(){
|
||||
List<MediaType> mediaTypes = new ArrayList<>();
|
||||
mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
|
||||
setSupportedMediaTypes(mediaTypes);
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
-5
@@ -6,6 +6,7 @@ import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWeb
|
||||
import org.springframework.boot.web.reactive.error.ErrorAttributes;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
|
||||
import org.springframework.web.reactive.function.server.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -32,7 +33,7 @@ public class JsonErrorWebExceptionHandler extends DefaultErrorWebExceptionHandle
|
||||
@Override
|
||||
protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
|
||||
Throwable error = super.getError(request);
|
||||
return responseError(this.buildMessage(request, error));
|
||||
return responseError(request, error);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,7 +51,8 @@ public class JsonErrorWebExceptionHandler extends DefaultErrorWebExceptionHandle
|
||||
*/
|
||||
@Override
|
||||
protected int getHttpStatus(Map<String, Object> errorAttributes) {
|
||||
return HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||
Integer httpStatus = (Integer) errorAttributes.remove("httpStatus");
|
||||
return httpStatus != null ? httpStatus : HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,14 +76,26 @@ public class JsonErrorWebExceptionHandler extends DefaultErrorWebExceptionHandle
|
||||
|
||||
/**
|
||||
* 构建返回的JSON数据格式
|
||||
* @param errorMessage 异常信息
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, Object> responseError(String errorMessage) {
|
||||
private Map<String, Object> responseError(ServerRequest request, Throwable error) {
|
||||
String errorMessage = buildMessage(request, error);
|
||||
int httpStatus = getHttpStatus(error);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("resp_code", 1);
|
||||
map.put("resp_msg", errorMessage);
|
||||
map.put("datas", null);
|
||||
map.put("httpStatus", httpStatus);
|
||||
return map;
|
||||
}
|
||||
|
||||
private int getHttpStatus(Throwable error) {
|
||||
int httpStatus;
|
||||
if (error instanceof InvalidTokenException) {
|
||||
InvalidTokenException ex = (InvalidTokenException)error;
|
||||
httpStatus = ex.getHttpErrorCode();
|
||||
} else {
|
||||
httpStatus = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||
}
|
||||
return httpStatus;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user