@FeignClient可以扩展-和@RestController实现-一个通用的,带批注的接口? [英] can @FeignClient extend - and @RestController implement - a common, fully-annotated Interface?

查看:1349
本文介绍了@FeignClient可以扩展-和@RestController实现-一个通用的,带批注的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望一个Feign客户端使用一个Spring Boot控制器,并且希望它们之间的协定在一个公共接口中尽可能地指定.

I want a Feign client to consume a Spring Boot controller, and I want the contract between them to be specified in a common Interface to the degree possible.

与method的接口如下所示:

The interface with method would look something like this:

@RequestMapping
public interface RuleManager {

    @RequestMapping(value = "/addRule", method = RequestMethod.POST, consumes = {"application/json"}, produces = {"application/json"})
    @ResponseBody Rule addRule(@RequestBody Rule rule);
}

Feign客户看起来像:

The Feign client would look like:

@FeignClient(url = "http://localhost:8080")
public interface RuleManagerClient extends RuleManager { }

和Spring引导控制器:

and the Spring boot controller:

@RestController
public class RuleManagerService implements RuleManager {

    @Override
    @Transactional
    public Rule addRule(@RequestBody Rule rule) {
        return rule;
    }
}

很好,我不必在两个地方指定@RequestMapping,但是不幸的是,似乎我必须两次指定@RequestBody.当从控制器或共享接口中省略@RequestBody时,将实例化Rule对象,但所有成员均设置为null.

It's nice that I don't have to specify @RequestMapping in two places, but unfortunately it seems I do have to specify @RequestBody twice. When @RequestBody is omitted from either the controller or the shared interface, the Rule object is instantiated but with all members set to null.

有没有解决的办法?也许这是在较新的版本中解决的?我的依赖项包括:

Is there a way around this ? Perhaps this is addressed in a newer version ? My dependencies include:

    <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:

I discovered this technique required at least feign-core 8.6 here:

https://jmnarloch.wordpress.com/2015/08/19/spring-cloud-designing-feign-client/

感谢您的帮助.

推荐答案

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

Apparently this does work--@RequestBody need only appear in the shared Interface. The problem was that I had the following property set in application.properties for the controller but not for the client:

spring.jackson.property-naming-strategy=CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES

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

That's why the object was instantiated on the server side but with all members null--effectively, the wrong properties were sent across the wire, for example "ruleName" instead of the expected "rule_name".

这篇关于@FeignClient可以扩展-和@RestController实现-一个通用的,带批注的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆