不支持 Netflix Feign 内容类型 'pplication/json;charset=UTF-8'

Netflix Feign Content type 'pplication/json;charset=UTF-8' not supported

我正在使用 Netflix Feign 制作我的 java http 客户端,使用这样的客户端:

public interface PromocodeClient {


@Headers({
    "Content-Type:" + MediaType.APPLICATION_JSON_UTF8_VALUE, "userIdentifier: {userIdentifier}"
})
@RequestLine("POST /rootpath/{code}/unblock")
Boolean unblock(
    @Param("userIdentifier") String userIdentifier,
    @Param("code") String promocode,
    BookingDTO booking);


static PromocodeClient connect() {
    return Feign.builder()
        .encoder(new GsonEncoder())
        .decoder(new GsonDecoder())
        .target(PromocodeClient.class, Urls.SERVICE_URL.toString());
         //Url.SERVICE_URL = http://localhost:8082/1.0
}

我收到一个奇怪的错误

{
  "timestamp" : "2016-08-02T07:47:16.208+0000",
  "status" : 415,
  "error" : "Unsupported Media Type",
  "exception" : "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message" : "Content type 'pplication/json;charset=UTF-8' not supported",
  "path" : "/1.0/rootpath/COD_PROMOCODE/unblock"
} 
}

消息显示 "Content type 'pplication/json;charset=UTF-8' not supported" 但我正在使用 Spring 的 MediaType.APPLICATION_JSON_UTF8_VALUE,其值为

application/json;charset=UTF-8

有人知道发生了什么事吗?

您可能需要在冒号后添加一个 space:

"Content-Type: " + MediaType.APPLICATION_JSON_UTF8_VALUE
              ^ here

HTTP standard不需要:

The field value MAY be preceded by any amount of [linear whitespace], though a single [space] is preferred.

但您正在与之通信的服务器可能实际上并不完全符合 HTTP 标准,因此类似的情况:

header.substring(header.indexOf(':') + 2)

找到 header 的值,它只处理 "preferred" 的情况。