SpringCloud openfeign @FeignClient注解的path参数不起作用?

发布于 2022-09-12 00:38:40 字数 1935 浏览 32 评论 0

本地测试时发现,请求http://localhost:9999/addLabel是可以的,
请求http://localhost:9999/cms/addLabel会报404。

接口是这样写的

@FeignClient(name = "image-server" path = "/cms")  
public interface ImageLabelServiceCmsFeign {  
  
  @PostMapping(value = "/addLabel")  
  Result<Void> addLabel(  
            @RequestParam(name = "parentLabelId") Integer parentLabelId,  
            @RequestParam(name = "labelName") String labelName);
}

服务实现是这样的

@RestController
public class ImageLabelServiceCmsFeignImpl implements ImageLabelServiceCmsFeign {
    @Override  
    public Result<Void> addLabel(Integer parentLabelId, String labelName) {
        // ...
    }
}

启动类是这样的

@EnableFeignClients(basePackages = {"com.xx"})  
@SpringBootApplication  
public class Application implements InitializingBean {  
  
 public static void main(String[] args) {  
  
    new SpringApplicationBuilder().sources(Application.class)  
            .web(WebApplicationType.SERVLET)  
            .run(args);  
    }
}

项目的依赖是这样的,使用了eureka作为服务注册中心。

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-web</artifactId>  
    <version>2.1.0.RELEASE</version>  
</dependency>

<!-- spring cloud -->  
<dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
    <version>2.1.0.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-openfeign</artifactId>  
    <version>2.1.0.RELEASE</version>  
</dependency>

有大佬解决过这个问题吗,或者能否给下思路。感激不尽啊

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩心态 2022-09-19 00:38:40

FeignClient是一个“客户端”,它是用来发起请求的,不是提供服务的。
你配置FeignClient的path只影响它去请求的地址,不影响服务的地址,如果你的服务路径没有那个/cms自然就请求不到了

接口改成这样,实现不变(推荐)

@FeignClient(name = "image-server")//①去掉path参数
@RequestMapping("/cms")//②增加注解
public interface ImageLabelServiceCmsFeign {  
  
  @PostMapping(value = "/addLabel")  
  Result<Void> addLabel(  
            @RequestParam(name = "parentLabelId") Integer parentLabelId,  
            @RequestParam(name = "labelName") String labelName);
}

或者接口不变,实现类加@RequestMapping("/cms")也可以


@FeignClient的path参数只对FeignClient的请求路径起作用,不会对restcontroller实现起作用,而接口上的requestMapping(包括衍生的GetMapping之类)会同时对FeignClient和Restcontroller起作用

不加path参数的话FeignClient的请求路径和服务的路径是一致的,可以调用成功。加了path="/cms"参数后,要想两边路径一致,就需要在实现上加@RequestMapping("/cms")或配置项目的context-path=/cms使请求路径和服务路径一致

铜锣湾横着走 2022-09-19 00:38:40

在接口的实现那里加上@RequestMapping("/cms"),
就为那个接口的所有url都添加上/cms的前缀了。
有点懵,接口上的那个path是做什么用的

@RestController  
@RequestMapping("/cms")  
public class ImageServiceCmsFeignImpl implements ImageServiceCmsFeign {
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文