OpenFeign的get方法传递对象的两种方式

  1. 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);
}
  1. 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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值