@FeignClient 可以扩展 - 和 @RestController 实现 - 一个通用的,完全注释的接口吗?

can @FeignClient extend - and @RestController implement - a common, fully-annotated Interface?

我希望 Feign 客户端使用 Spring 引导控制器,并且我希望它们之间的契约尽可能在公共接口中指定。

带有方法的接口看起来像这样:

@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/

感谢您的帮助。

显然这确实有效——@RequestBody 只需要出现在共享接口中。问题是我在 application.properties 中为控制器设置了以下 属性,但没有为客户端设置:

spring.jackson.property-naming-strategy=CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES

这就是对象在服务器端实例化但所有成员都为空的原因——实际上,错误的属性通过网络发送,例如 "ruleName" 而不是预期的 "rule_name"。