【问题标题】:Netflix Feign Content type 'pplication/json;charset=UTF-8' not supported不支持 Netflix Feign 内容类型 'application/json;charset=UTF-8'
【发布时间】:2016-12-07 11:36:32
【问题描述】:

我正在使用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"
} 
}

消息显示“不支持内容类型'应用程序/json;charset=UTF-8'”但我使用的是Spring 的MediaType.APPLICATION_JSON_UTF8_VALUE 哪个值是

应用程序/json;charset=UTF-8

有人知道发生了什么吗?

【问题讨论】:

  • 在冒号后面加一个空格可以吗? "Content-Type: " + ...(应该没有必要;但服务器可能会做一些奇怪的事情,比如找到 : 并移动 2 个字符以找到值的开头)。

标签: java spring-cloud spring-cloud-netflix netflix-feign


【解决方案1】:

冒号后可能需要一个空格:

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

HTTP standard 不需要:

字段值前面可以有任意数量的[线性空白],但最好使用单个[空格]。

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

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

查找标头的值,它仅处理“首选”情况。

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2015-03-26
    • 2019-12-16
    • 2020-03-20
    相关资源
    最近更新 更多