spring-boot - Spring/Eureka/Feign - FeignClient 将 Content-Type header 设置为 application/x-www-form-urlencoded

标签 spring-boot spring-cloud netflix-feign netflix-eureka

当我使用 FeignClient它正在设置 Content-Typeapplication/x-www-form-urlencoded而不是 application/json;charset=UTF-8 .

如果我使用 RestTemplate发送相同的消息消息头Content-Type正确设置为 application/json;charset=UTF-8 .
FeignClientRestTemplate正在使用 Eureka对于服务发现,我通过调试服务器收到的HTTP消息发现了这个问题。

服务器端的 Controller 如下所示:

@RestController
@RequestMapping("/site/alarm")
public class SiteAlarmController {
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<RaiseAlarmResponseDto> raiseAlarm(@RequestBody RaiseSiteAlarmRequestDto requestDto) {
        ...
    }

我的 FeignClient调用警报的服务中的界面如下所示:
@FeignClient("alarm-service")
public interface AlarmFeignService {
    @RequestMapping(method = RequestMethod.POST, value = "/site/alarm")
    RaiseAlarmResponseDto raiseAlarm(@RequestBody RaiseSiteAlarmRequestDto requestDto);
}
FeignClient 中的 HTTP 消息头是:
Accept: */*
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.7.0_60
Host: smit005s-MacBook-Pro.local:9120
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 323

报警服务不喜欢 Content-Type并引发以下异常:
2015-04-22 12:12:28.580 thread="qtp1774842986-25" class="org.eclipse.jetty.servlet.ServletHandler" level="WARN" 
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is feign.FeignException: status 415 reading AlarmFeignService#raiseAlarm(RaiseSiteAlarmRequestDto); content:
{"timestamp":1429701148576,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Unsupported Media Type","path":"/site/alarm"}
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) ~[tomcat-embed-core-8.0.20.jar:8.0.20]
    ...
    ... /* commented rest of stack out */
    ...

如果我将客户端代码更改为使用 RestTemplate如下:
@Service
public class AlarmService {
    @Autowired
    private RestTemplate restTemplate;
...
    public void send(RaiseSiteAlarmRequestDto alarm) {
        RaiseAlarmResponseDto result = restTemplate.postForObject("http://alarm-service/site/alarm", 
            raiseSiteAlarmRequestDto, RaiseAlarmResponseDto.class);
    }
}

它适用于 RestTemplate , alarm-service收到消息并成功处理。 RestTemplate 发送的消息头是:
Accept: application/json, application/*+json
Content-Type: application/json;charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.7.0_60
Host: smit005s-MacBook-Pro.local:9120
Connection: keep-alive
Content-Length: 323

最佳答案

答案是按照@spencergibb 的建议去做;使用 consumes @RequestMapping 中的指令FeignClient 上的注释界面。这个Spring/Netflix documentaition也有一个例子。

例如 @FeignClient客户端中的接口(interface)声明现在是:

@FeignClient("alarm-service")
public interface AlarmFeignService {
    @RequestMapping(method = RequestMethod.POST, value = "/site/alarm", consumes = "application/json"))
    RaiseAlarmResponseDto raiseAlarm(RaiseSiteAlarmRequestDto requestDto);
}

请注意,这仅在客户端是必需的,服务器端 Controller 不需要进行此更改。

如果在 @FeignClient 上默认这样做会很好然后它将与 RestTemplate 一致和服务器端 Controller @RequestMapping注解。也许这可以在 spring-cloud 的 future 版本中完成。 .

关于spring-boot - Spring/Eureka/Feign - FeignClient 将 Content-Type header 设置为 application/x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798212/

相关文章:

java - 扩展 Spring Cloud Config Client 的正确方法是什么?

java - 带有 Date 类型的 spring-cloud-feign Client 和 @RequestParam

java - Feign 客户端出现“ body 参数过多”异常

javascript - react + Spring 启动: Can't get Authorization value from Header

java - Spring boot(data) heisenbug 与检测数据库驱动程序

java - 如何在调试/运行 Spring Boot 项目时在 IntelliJ "out"目录中生成 build-info.properties?

azure - Azure应用程序配置Web Hook 是否通过http调用更新应用程序的所有实例?

spring-boot - Kubernetes-通过minikube中的Spring Boot应用程序连接Elasticsearch

java - 运行测试时禁用 Spring Cloud Consul

netflix-feign - http代码为401时使用spring cloud feign,response.body()为null