feign nan转换报错
时间: 2023-10-19 07:51:57 浏览: 83
当使用Feign进行远程调用时,如果服务端返回的结果中存在NaN(不是一个数字)类型的数据,Feign默认是无法将其正确转换的,会抛出转换错误的异常。
解决这个问题的方式是通过自定义Feign的解码器,在解码器中处理NaN类型的数据转换。下面是一个示例的自定义解码器的代码:
```java
import feign.FeignException;
import feign.Response;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.io.IOException;
import java.lang.reflect.Type;
public class CustomDecoder implements Decoder {
private final ResponseEntityDecoder responseEntityDecoder;
public CustomDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
this.responseEntityDecoder = new ResponseEntityDecoder(new Decoder.Default(), messageConverters);
}
@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
if (response.status() == HttpStatus.OK.value()) {
ResponseEntity<?> entity = (ResponseEntity<?>) responseEntityDecoder.decode(response, type);
Object body = entity.getBody();
// 处理NaN转换
if (body instanceof Double && ((Double) body).isNaN()) {
return null; // 或者根据需求进行其他处理
}
return body;
}
return responseEntityDecoder.decode(response, type);
}
}
```
然后在FeignClient接口上添加`configuration`属性指定使用自定义解码器,例如:
```java
@FeignClient(value = "your-service", configuration = CustomDecoder.class)
public interface YourServiceClient {
// ...
}
```
通过以上方式,你可以自定义处理Feign中NaN转换的问题。希望能帮到你!如果有更多问题,请继续提问。
阅读全文
相关推荐

















