我希望 Feign 客户端使用 Spring Boot 控制器,并且我希望它们之间的合同尽可能在通用接口中指定。
带有方法的接口看起来像这样:
@RequestMapping
public interface RuleManager {
@RequestMapping(value = "/addRule", method = RequestMethod.POST, consumes = {"application/json"}, produces = {"application/json"})
@ResponseBody Rule addRule(@RequestBody Rule rule);
}
Feign 客户端看起来像:
@FeignClient(url = "http://localhost:8080")
public interface RuleManagerClient extends RuleManager { }
和 Spring 引导控制器:
@RestController
public class RuleManagerService implements RuleManager {
@Override
@Transactional
public Rule addRule(@RequestBody Rule rule) {
return rule;
}
}
很高兴我不必在两个地方指定@RequestMapping,但不幸的是,我似乎必须指定@RequestBody 两次。当控制器或共享接口中省略 @RequestBody 时,将实例化 Rule 对象,但所有成员都设置为 null。
有没有解决的办法 ?也许这在较新的版本中得到解决?我的依赖项包括:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<exclusions>
<exclusion>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<version>8.14.3</version>
</dependency>
我在这里发现这种技术至少需要 feign-core 8.6:
https://jmnarloch.wordpress.com/2015/08/19/spring-cloud-designing-feign-client/
谢谢你的帮助。