新增更新包识别
parent
c83101bfdf
commit
ab9ea38e0a
@ -0,0 +1,51 @@
|
||||
package com.docus.server.common.config;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.docus.infrastructure.web.exception.ApiException;
|
||||
import feign.FeignException;
|
||||
import feign.Response;
|
||||
import feign.Util;
|
||||
import feign.codec.Decoder;
|
||||
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Objects;
|
||||
|
||||
//feign处理统一输出和异常
|
||||
public class FeignResponseDecoder extends ResponseEntityDecoder {
|
||||
public FeignResponseDecoder(Decoder decoder) {
|
||||
super(decoder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object decode(Response response, Type type) throws IOException, FeignException {
|
||||
String body;
|
||||
//response. body默认是InputStreamBody , 加了Feign 1og之后转为ByteArrayBody
|
||||
if (response.body().length() == null && response.body().asInputStream().available() == 0) {
|
||||
byte[] bodyData = Util.toByteArray(response.body().asInputStream());
|
||||
body = new String(bodyData);
|
||||
} else {
|
||||
body = response.body().toString();
|
||||
}
|
||||
if (!StringUtils.isEmpty(body)) {
|
||||
JSONObject responseobject = JSON.parseObject(body);
|
||||
if (responseobject.containsKey("code")) {
|
||||
Integer code = responseobject.getInteger("code");
|
||||
if (!Objects.equals(0, code)) {
|
||||
String message = responseobject.getString("msg");
|
||||
throw new ApiException(code, message);
|
||||
}
|
||||
String result = responseobject.getString("data");
|
||||
if (result == null || "null".equalsIgnoreCase(result)) {
|
||||
return null;
|
||||
}
|
||||
response = response.toBuilder().body(result, Charset.forName("UTF-8")).build();
|
||||
}
|
||||
}
|
||||
return super.decode(response, type);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.docus.server.common.config;
|
||||
|
||||
import feign.codec.Decoder;
|
||||
import feign.optionals.OptionalDecoder;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.cloud.openfeign.support.SpringDecoder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SpringCloudConfig {
|
||||
|
||||
@Autowired(required = false)
|
||||
private ObjectFactory<HttpMessageConverters> messageConverters;
|
||||
|
||||
//feign统一输出体响应拦截
|
||||
@ConditionalOnMissingBean(value = Decoder.class)
|
||||
@Bean
|
||||
public Decoder feignResponseDecoder() {
|
||||
return new OptionalDecoder(
|
||||
new FeignResponseDecoder(new SpringDecoder(this.messageConverters)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue