springcloud中使用feign对服务间的相互调用教程和报错解决
使用feign
在使用feign过程中遇到很多报错,所以分享出来,防止入坑
本人项目是需要一个house服务调用oauth中的用户的信息
主要微服务如下:
-eureka
-zuul
-oauth
-house
第一步、house导入依赖(需要使用其他服务接口的服务中导入)
首先要把house服务注册到eureka和配置到注册中心,然后才有以下操作
<!-- springcloud-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
house启动类(需要使用其他服务接口的服务中导入)
@EnableFeignClients
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ServiceHouseApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHouseApplication.class,args);
}
}
只要代码不报红feign的配置就完成了
编写接口
@FeignClient(value = "service-oauth")
public interface UserFeignClient {
@RequestMapping(value = "/user/page", method = RequestMethod.GET)
public ApiResult queryUserPage(@RequestParam("realname") String realname, @RequestParam("pageSize") Integer pageSize);
}
在controller中使用
@Autowired
private UserFeignClient userFeignClient;
@RequestMapping(value = "page", method = RequestMethod.GET)
public ApiResult queryUserPage(@RequestParam("realname") String realname, @RequestParam("pageSize") Integer pageSize){
ApiResult apiResult = userFeignClient.queryUserPage(realname,pageSize);
if (apiResult.getData()!=null){
return apiResult;
}
return ApiResult.fail(1003 ,"response empty");
}
到现在为止,整个项目feign使用基础搭建就完成了,下面开始报错
第一个错:
报了一个mapper重复的错,那个错找不到了,所以这里说一下,是因为分页插件导入的依赖冲突,后面删除依赖后重新导了一个依赖就成功了,这个问题耽误了半天,希望你们别遇到
第二个错,feign启动空指针异常,swagger版本导致:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘houseUserController’: Unsatisfied dependency expressed through field ‘userFeignClient’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘com.nbmssoft.feign.UserFeignClient’: FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException: null
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'houseUserController': Unsatisfied dependency expressed through field 'userFeignClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.nbmssoft.feign.UserFeignClient': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException: null
这是因为我的swagger用的是2.2.2,更改swagger2.9.2可以解决
<!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
第三个错,参数问题 Method has too many Body parameters
异常原因:当使用Feign时,如果发送的是get请求,那么需要在请求参数前加上@RequestParam注解修饰,Controller里面可以不加该注解修饰 ,@RequestParam可以修饰多个,@RequestParam是用来修饰参数,不能用来修饰整个对象。
再看一眼 controller
interface
post请求
feign中可以有多个@RequestParam,但只能有一个@RequestBody,这两个注解不做多的解释,可以百度
我请求的oauth微服务controller,这里大家可以自己多理解一下什么原理
上面的realname包含在UserInfoForManage中,pageSize包含在PageInfo中,两个实体类我都重写在新服务中,因为我house服务会用到
参数报错参考地址:https://blog.csdn.net/haishiyizhenfeng/article/details/80607003