- springboot版本在2.1之前,封装成map调用feign方法
例子如下:A服务调用B服务
//A-service
@GetMapping("/maps")
public ReturnEntity getUserMap(UserDto userDto) {
HashMap map = new HashMap(BeanMap.create(userDto));
ReturnEntity returnEntity = sysLoginFeignClient.getUserMap(map);
return returnEntity;
}
//feign-service
@FeignClient(contextId="userService",value="${user-name}",fallbackFactory = RemoteSystemFallbackFactory.class)
public interface FeigndService {
@GetMapping(value = "/user/maps")
ReturnEntity getUserMap(@RequestParam Map map);
}
//B-service
@GetMapping("/maps")
public List<UserDto > getUserMap(UserDto userDto) {
return sysDeptService.getUserMap(userDto);
}
- springboot版本在2.1之后,使用
@SrpingQueryMap
注解
//A-service
@GetMapping("/maps")
public ReturnEntity getUserMap(UserDto userDto) {
ReturnEntity returnEntity = sysLoginFeignClient.getUserMap(userDto);
return returnEntity;
}
@FeignClient(contextId="userService",value="${user-name}",fallbackFactory = RemoteSystemFallbackFactory.class)
public interface FeigndService {
@GetMapping(value = "/user/maps")
ReturnEntity getUserMap(@SpringQueryMap UserDto userDto);
}
//B-service
@GetMapping("/maps")
public List<UserDto > getUserMap(UserDto userDto) {
return sysDeptService.getUserMap(userDto);
}
@SpringQueryMap
存在的问题:无法解析父类的属性,会导致类型不一致报错
Failed to convert value of type 'xxx' to required type 'xxx'
解决方案:添加feign配置类,并在@feignClient
注解上指定配置类
@Configuration
public class FeignConfiguration {
/**
* @Description
* @date 2019/5/21 17:27
* @version V1.0.0
*/
@Bean
Logger.Level feignLoggerLevel() {
//这里记录所有,根据实际情况选择合适的日志level
return Logger.Level.FULL;
}
/**
* @Description 替换解析queryMap的类,实现父类中变量的映射
* @date 2019/5/21 16:59
* @version V1.0.0
*/
@Bean
public Feign.Builder feignBuilder() {
return Feign.builder()
.queryMapEncoder(new BeanQueryMapEncoder())
.retryer(Retryer.NEVER_RETRY);
}
}
-------------------------------------------------------------------------
@FeignClient(contextId="userService",value="${user-name}",configuration = FeignConfiguration.class)
public interface FeigndService {
@GetMapping(value = "/user/maps")
ReturnEntity getUserMap(@SpringQueryMap UserDto userDto);
}