问题一:无法添加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);
}