使用spring cloud feign,当http代码为401时,respose.body()为null

vin*_*ren 6 netflix-feign spring-cloud-feign

使用spring cloud feign调用我的服务,当服务返回401异常时,respose.body()为null。

\n\n

当我抛出异常时,throw new BadRequestException(400\xef\xbc\x8c \xe2\x80\x9cthis http code is 400\xe2\x80\x9d);我可以得到错误消息,this http code is 400通过response.body()。\n但是当我抛出异常时throw new BadRequestException(401\xef\xbc\x8c \xe2\x80\x9cthis http code is 401\xe2\x80\x9d);response.body()为空。\n这个响应是feign.Response

\n\n

为什么当 http 代码为 401 时我无法收到此错误消息?

\n\n

希望您的帮助!非常感谢!

\n

小智 2

切换到 feign-okhttp 将解决您的问题。

在 Pom 中添加:

<dependency>
    <groupId>com.netflix.feign</groupId>
    <artifactId>feign-okhttp</artifactId>
    <version>8.18.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

并添加到您的配置中:

feign.okhttp.enabled: true
Run Code Online (Sandbox Code Playgroud)