springcloud 使用Feign遇到的坑

问题一:无法添加spring-cloud-starter-feign依赖

  • 解决办法:
   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>

问题二:编写@FeignClient接口出现请求方式错误,参数错误

  • 解决办法
package com.example.eurekaconsumerclient.dao;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Component
@FeignClient("client-provider")
public interface TestClient {
    //必须使用@RequestMapping,不能使用@GetMapping
    //@PathVariable 必须指定参数名称
    @RequestMapping(method = RequestMethod.GET,value = "/test/{name}")
    String test(@PathVariable("name") String name);
}

问题三:在@FeignClient接口中,方法参数是复杂对象,即使指定请求格式为Get,Feign依然会以post方法进行发送请求,控制台报错

  • 解决办法
package com.example.eurekaconsumerclient.dao;
import com.example.eurekaconsumerclient.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@Component
@FeignClient("client-provider")
public interface TestClient {
    //必须使用@RequestMapping,不能使用@GetMapping
    //@PathVariable 必须指定参数名称
    @RequestMapping(method = RequestMethod.GET,value = "/test/{name}")
    String test(@PathVariable("name") String name);
    /**
     * 错误写法
     * @param user User 对象
     * @return
     */
    @RequestMapping(method = RequestMethod.Get,value = "/test/")
    User findByUser(User user);
    /**
     * 正确写法
     * @param id    User->id属性
     * @param username   User->username属性
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/test/")
    User findByUser(@RequestParam("id") Long id,@RequestParam("username") String username);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值