feign调用返回object类型转换方式

feign调用返回object类型转换 引入依赖

com.fasterxml.jackson.corejackson-databind2.9.8

/*** @Description: 将数据转换到相应的容器* @param bean* @param clazz* @return* @throws* @author dlh* @date 2019/3/19 10:28*/ public static T convertValue(Object bean, Class clazz){try{ObjectMapper mapper = new ObjectMapper(); return mapper.convertValue(bean, clazz); }catch(Exception e){logger.error("错误的转换:BeanUtil.convertValue() --->" + e.getMessage()); return null; } }

SpringCloud feign接口转换服务 【feign调用返回object类型转换方式】Feign是通过提供面向接口操作的方式来替代RestTemplate API的Rest操作。
使用Feign
Feign这种技术应用在服务消费端
修改pom.xml配置文件,加入Feign的依赖包
org.springframework.cloudspring-cloud-starter-feign

由于Fegin是将Rest的操作转换成接口的形式,所以我们需要新建一个接口,并在接口上声明@FeignClient注解
@FeignClient(value = "https://www.it610.com/article/DEPT-PROVIDER",configuration = FeignClientConfig.class)public interface DeptClientService {@RequestMapping(method= RequestMethod.GET,value="https://www.it610.com/dept/get/{id}")public Dept get(@PathVariable("id") long id) ; @RequestMapping(method=RequestMethod.GET,value="https://www.it610.com/dept/list")public List list() ; @RequestMapping(method=RequestMethod.POST,value="https://www.it610.com/dept/add")public boolean add(Dept dept) ; }@Configurationpublic class FeignClientConfig {@Beanpublic Logger.Level getFeignLoggerLevel() {return feign.Logger.Level.FULL ; }@Beanpublic BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {return new BasicAuthRequestInterceptor("admin", "admin"); }}

其中configuration = FeignClientConfig.class不是必须的,将configuration属性去除仍然能work。
将之前的Rest操作的API,替换成面向DeptClientService接口的形式
@RestController@RequestMapping("/consumer/dept")public class ConsumerDeptController {@Autowiredprivate DeptClientService deptClientService; @RequestMapping(value = "https://www.it610.com/get")public Dept get(long id) {return this.deptClientService.get(id); }@RequestMapping("/list")public List list(){return this.deptClientService.list(); }@RequestMapping("/add")public boolean add(Dept dept){return this.add(dept); }/*public static final String DEPT_GET_URL = "http://DEPT-PROVIDER/dept/get/"; public static final String DEPT_LIST_URL = "http://DEPT-PROVIDER/dept/list/"; public static final String DEPT_ADD_URL = "http://DEPT-PROVIDER/dept/add"; @Autowiredprivate RestTemplate restTemplate; @Autowiredprivate HttpHeaders httpHeaders; @Autowiredprivate LoadBalancerClient loadBalancerClient; @RequestMapping(value = "https://www.it610.com/get")public Dept get(long id) {ServiceInstance serviceInstance = this.loadBalancerClient.choose("DEPT-PROVIDER") ; System.out.println("【*** ServiceInstance ***】host = " + serviceInstance.getHost()+ "、port = " + serviceInstance.getPort()+ "、serviceId = " + serviceInstance.getServiceId()); //Dept dept = restTemplate.getForObject(DEPT_GET_URL + id, Dept.class); Dept dept = restTemplate.exchange(DEPT_GET_URL+id, HttpMethod.GET,new HttpEntity(this.httpHeaders),Dept.class).getBody(); return dept; }@RequestMapping("/list")public List list(){//List deptList = restTemplate.getForObject(DEPT_LIST_URL, List.class); List deptList = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,new HttpEntity(this.httpHeaders),List.class).getBody(); return deptList; }@RequestMapping("/add")public boolean add(Dept dept){//Boolean flag = restTemplate.postForObject(DEPT_ADD_URL, dept, Boolean.class); Boolean flag = this.restTemplate.exchange(DEPT_ADD_URL,HttpMethod.POST,new HttpEntity(this.httpHeaders),Boolean.class).getBody(); return flag; }*/}
在启动类中加入@EnableFeignClients注解
@SpringBootApplication@EnableEurekaClient@EnableFeignClients(basePackages = {"cn.zgc.service"})public class FeignConsumer_80_StartSpringCloudApplication {public static void main(String[] args) {SpringApplication.run(FeignConsumer_80_StartSpringCloudApplication.class,args); }}

Feign自带了负责均衡特性,所以使用Feign之后可以不用使用Ribbon。
Feign的配置
Feign 最重要的功能就是将 Rest 服务的信息转换为接口,但是在实际的使用之中也需要考虑到一些配置情况,例如:数据压缩, Rest 的核心本质在于: JSON 数据传输( XML、文本),于是就必须思考一种情况,万一用户发送的数据很大呢? 所以这个时候可以考虑修改application.yml 配置文件对传输数据进行压缩;
feign: compression: request: mime-types:# 可以被压缩的类型 - text/xml - application/xml - application/json min-request-size: 2048 # 超过2048的字节进行压缩

开启Feign的日志(默认是不开启的)
logging: level: cn.zgc.service: DEBUG

feign调用的时候返回参数不是期望的数据类型 今天碰到个坑,feign调用其他应用的时候反参并不是期望的类型;不知道这个在其他请求方式有没有这个bug
解决的方法
是在feign调用的那里指定Response的泛型类,我这边是碰到期望是返回Long类型,但是返回的是Integer类型,不知道是不是隐式转换了,这里记录一下这个Bug
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读