gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:54:06 85 4
gpt4 key购买 nike

当我使用 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/

85 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com