Spring / Eureka / Feign - FeignClient设置Content-Type标题到application / x-www-form-urlencoded

时间:2015-04-22 12:53:37

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

当我使用FeignClient时,它会将Content-Type设置为application/x-www-form-urlencoded而不是application/json;charset=UTF-8

如果我使用RestTemplate发送相同的邮件,邮件标头Content-Type已正确设置为application/json;charset=UTF-8

FeignClientRestTemplate都使用Eureka进行服务发现,我通过调试服务器收到的HTTP消息发现了这个问题。

服务器端的控制器如下所示:

@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);
    }
}

它适用于RestTemplatealarm-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

1 个答案:

答案 0 :(得分:9)

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

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

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

请注意,这仅在客户端需要,服务器端控制器不需要进行此更改。

如果在@FeignClient上默认完成此操作会很好,那么它将与RestTemplate和服务器端控制器@RequestMapping注释保持一致。也许这可以在spring-cloud的未来版本中完成。