Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Spring/Eureka/Feign-FeignClient将内容类型头设置为application/x-www-form-urlencoded_Spring Boot_Spring Cloud_Netflix Feign_Netflix Eureka - Fatal编程技术网

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]也不适用于我