首页 > 解决方案 > OpenFeign 客户端不应用“Content-Type”标头值:415 作为响应

问题描述

我想使用 feign 执行 DELETE:

public static <T> T createClient(Class<T> type) {
    return Feign.builder()
            .client(new OkHttpClient())
            .encoder(new GsonEncoder())
            .decoder(new CustomGsonDecoder())
            .logger(new Slf4jLogger(type))
            .logLevel(Logger.Level.FULL)
            .target(type, url);
}

来自 ConsumersClient 接口的方法:

@RequestLine("DELETE /consumers/{id}")
@Headers({"Content-Type: application/json", "Authorization: Bearer {token}"})
Response deleteConsumerById(@Param("token") String token, @Param("id") String id);

当我这样做时:

CLIENT = createClient(ConsumersClient.class);
CLIENT.deleteConsumerById(token, id)

我得到了

{"statusCode":415,"error":"{\"timestamp\":\"2020-02-27T08:09:33.634Z\",\"status\":415,\"error\":\"Unsupported Media Type\",\"message\":\"Content type '' not supported\",\"path\":\"/consumers/id\"}"}

由于消息是:““不支持内容类型”,我假设 Feign 没有将 Content-Type 值应用于其标题。

请注意:通过 Postman 的相同请求返回 200 并具有相同的参数。因此,该服务按预期工作。

Logback 说标题包括:

11:09:32.660 [main] DEBUG ConsumersClient - [ConsumersClient#deleteConsumerById] ---> DELETE https://hereGoesMyURL/consumers/id HTTP/1.1
11:09:32.663 [main] DEBUG ConsumersClient - [ConsumersClient#deleteConsumerById] Content-Type: application/json
11:09:32.663 [main] DEBUG ConsumersClient - [ConsumersClient#deleteConsumerById] Authorization: Bearer hereGoesTheToken
11:09:32.664 [main] DEBUG ConsumersClient - [ConsumersClient#deleteConsumerById] ---> END HTTP (0-byte body)

请帮助找出我的代码有什么问题。

标签: javahttp-headershttp-deletefeignopenfeign

解决方案


该问题已在此处讨论: https ://github.com/OpenFeign/feign/issues/391

由于没有为带有主体的 DELETE 定义语义,因此 Feign 正在剥离此类请求的内容类型标头。


推荐阅读