Spring boot Spring/Eureka/Feign-FeignClient将内容类型头设置为application/x-www-form-urlencoded
当我使用Spring boot Spring/Eureka/Feign-FeignClient将内容类型头设置为application/x-www-form-urlencoded,spring-boot,spring-cloud,netflix-feign,netflix-eureka,Spring Boot,Spring Cloud,Netflix Feign,Netflix Eureka,当我使用佯装客户端时,它将内容类型设置为application/x-www-form-urlencoded,而不是application/json;字符集=UTF-8 如果我使用restemplate发送相同的消息,消息头Content-Type被正确设置为application/json;字符集=UTF-8 FeignClient和restemplate都在使用Eureka进行服务发现,我通过调试服务器接收的HTTP消息发现了这个问题 服务器端的控制器如下所示: @RestController
佯装客户端时,它将内容类型设置为application/x-www-form-urlencoded
,而不是application/json;字符集=UTF-8
如果我使用restemplate
发送相同的消息,消息头Content-Type
被正确设置为application/json;字符集=UTF-8
FeignClient
和restemplate
都在使用Eureka
进行服务发现,我通过调试服务器接收的HTTP消息发现了这个问题
服务器端的控制器如下所示:
@RestController
@RequestMapping("/site/alarm")
public class SiteAlarmController {
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<RaiseAlarmResponseDto> raiseAlarm(@RequestBody RaiseSiteAlarmRequestDto requestDto) {
...
}
@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
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
报警服务不喜欢内容类型
,并引发以下异常:
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
一起工作,报警服务
接收消息并成功处理。restemplate
发送的消息头包括:
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
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
答案是按照@Spencergib的建议去做;在FeignClient
界面上的@RequestMapping
注释中使用consumes
指令。这也有一个例子
例如,客户机中的@FeignClient
接口声明现在是:
@FeignClient("alarm-service")
public interface AlarmFeignService {
@RequestMapping(method = RequestMethod.POST, value = "/site/alarm", consumes = "application/json"))
RaiseAlarmResponseDto raiseAlarm(RaiseSiteAlarmRequestDto requestDto);
}
注意,这仅在客户端是必需的,而服务器端控制器不需要进行此更改
如果这是在默认情况下在@FeignClient
上完成的,那么它将与restemplate
和服务器端控制器@RequestMapping
注释一致。也许这可以在未来的springcloud
版本中实现 您是否尝试过丢弃控制器的响应属性
,以测试这是否干扰了外部客户端?我的另一个猜测是,Faign无法将您的RaiseAlarmResponseDto
对象反序列化。在@FaignClient
上的@RequestBody
没有任何作用。你能用eureka成功地打电话吗?谢谢@Spencergib这是个好建议。将其更改为RestTemplate确实有效,它正在通过Eureka发现服务。当我将调试放在接收器端时,我可以看到外部客户端将其发送时,将内容类型
标题错误地设置为application/x-www-form-urlencoded
。而restemplate
将内容类型设置为application/json;字符集=UTF-8
。我将用这个更新问题。您是否尝试过将consumes=“application/json”
添加到@RequestMapping
上的@FeignClient
?是的,可以。但从我看到的示例来看,这应该是不必要的。它不适用于application/x-www-form-urlencoded,您将得到:feign.codec.EncodeException:无法写入请求:没有为请求类型[com.*.pojo.OAuthLoginPojo]找到合适的HttpMessageConverter,而内容类型[application/x-www-form-urlencoded]也不适用于我