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

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

我正在使用Netflix假装我的 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"
} 
}

消息显示“不支持内容类型 'pplication/json;charset=UTF-8'”,但我正在使用 MediaType.APPLICATION_JSON_UTF8_VALUE Spring 的值为

application/json;charset=UTF-8

有人知道发生了什么吗?

最佳答案

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

"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 的值,该值仅处理“首选”情况。

关于java - Netflix Feign 不支持内容类型 'pplication/json;charset=UTF-8',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38714938/

相关文章:

java - 如何用信号量替换synchronized、wait、notify? (生产者消费者)

Spring cloud contracts插件更改源集

spring-cloud - Zuul 无法连接到 Eureka

spring - spring cloud config如何使用本地属性覆盖远程属性

spring-cloud - 容错和容错有什么区别?

spring-cloud - Spring Cloud Loadbalancer 可以与 Netflix Eureka 一起使用,而不是与 Netflix Ribbon 一起使用吗?

java - spring-boot - 哪段代码实际为 springMVC 注册了调度程序 servlet?

java - 递归查找给定长度的平方和的顺序

java - 如何在Java数据结构中实现多类别?

java - 为 Hystrix-AMQP 生成了太多日志