Spring Boot OpenFeign Get 请求 415 解决方法

Spring Boot OpenFeign About 3,329 words

OpenFeign Client

User对象想转为QueryString请求参数。

@FeignClient(name = "user-client", url = "http://localhost:8080")
public interface MyOpenFeign {

    @GetMapping("/user")
    Integer getUser(User user);

}

报错信息

但是等到报错信息415,不支持的Media Type

Caused by: feign.FeignException$UnsupportedMediaType: [415] during [GET] to [http://localhost:8080/user] [MyOpenFeign#getUser(UserRequest)]: [{"timestamp":"2022-11-02T02:45:27.940+00:00","status":415,"error":"Unsupported Media Type","path":"/user"}]
    at feign.FeignException.clientErrorStatus(FeignException.java:211) ~[feign-core-10.12.jar:na]
    at feign.FeignException.errorStatus(FeignException.java:177) ~[feign-core-10.12.jar:na]
    at feign.FeignException.errorStatus(FeignException.java:169) ~[feign-core-10.12.jar:na]
    at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92) ~[feign-core-10.12.jar:na]
    at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96) ~[feign-core-10.12.jar:na]
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-10.12.jar:na]
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.12.jar:na]
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.12.jar:na]
    at com.sun.proxy.$Proxy107.multipartUploadPartValid(Unknown Source) ~[na:na]

OpenFeign 详细日志

开启OpenFeign详细日志,发现QueryString变为了JSON格式上传,Content-Type也变为了application/json,所以路由到了另外一个接口上了。

[MyOpenFeign#getUser] ---> GET http://localhost:8080/user HTTP/1.1
[MyOpenFeign#getUser] Content-Length: 28
[MyOpenFeign#getUser] Content-Type: application/json
[MyOpenFeign#getUser] 
[MyOpenFeign#getUser] {"userId":"123","info":null}
[MyOpenFeign#getUser] ---> END HTTP (28-byte body)
[MyOpenFeign#getUser] <--- HTTP/1.1 415 (59ms)
[MyOpenFeign#getUser] accept: multipart/form-data
[MyOpenFeign#getUser] connection: keep-alive
[MyOpenFeign#getUser] content-type: application/json
[MyOpenFeign#getUser] date: Wed, 02 Nov 2022 03:08:46 GMT
[MyOpenFeign#getUser] keep-alive: timeout=60
[MyOpenFeign#getUser] transfer-encoding: chunked
[MyOpenFeign#getUser] 
[MyOpenFeign#getUser] {"timestamp":"2022-11-02T03:08:46.440+00:00","status":415,"error":"Unsupported Media Type","path":"/user"}
[MyOpenFeign#getUser] <--- END HTTP (106-byte body)

解决方法

添加OpenFeign中提供的@SpringQueryMap注解,使对象转为QueryString请求参数。

@FeignClient(name = "user-client", url = "http://localhost:8080")
public interface MyOpenFeign {

    @GetMapping("/user")
    Integer getUser(@SpringQueryMap User user);

}

查看OpenFeign日志,发现请求参数直接拼接在URL路径上了。

[MyOpenFeign#multipartUploadPartValid] ---> GET http://localhost:8080/user?userId=123 HTTP/1.1
[MyOpenFeign#multipartUploadPartValid] ---> END HTTP (0-byte body)
[MyOpenFeign#multipartUploadPartValid] <--- HTTP/1.1 200 (1346ms)
[MyOpenFeign#multipartUploadPartValid] connection: keep-alive
[MyOpenFeign#multipartUploadPartValid] content-type: application/json
[MyOpenFeign#multipartUploadPartValid] date: Wed, 02 Nov 2022 03:17:14 GMT
[MyOpenFeign#multipartUploadPartValid] keep-alive: timeout=60
[MyOpenFeign#multipartUploadPartValid] transfer-encoding: chunked
[MyOpenFeign#multipartUploadPartValid] 
[MyOpenFeign#multipartUploadPartValid] 0
[MyOpenFeign#multipartUploadPartValid] <--- END HTTP (1-byte body)
Views: 1,128 · Posted: 2023-02-20

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh