feign調(diào)用返回object類型轉(zhuǎn)換方式
feign調(diào)用返回object類型轉(zhuǎn)換
引入依賴
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
/**
* @Description: 將數(shù)據(jù)轉(zhuǎn)換到相應(yīng)的容器
* @param bean
* @param clazz
* @return
* @throws
* @author dlh
* @date 2019/3/19 10:28
*/
public static <T> T convertValue(Object bean, Class<T> clazz){
try{
ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(bean, clazz);
}catch(Exception e){
logger.error("錯誤的轉(zhuǎn)換:BeanUtil.convertValue() --->" + e.getMessage());
return null;
}
}
SpringCloud feign接口轉(zhuǎn)換服務(wù)
Feign是通過提供面向接口操作的方式來替代RestTemplate API的Rest操作。
使用Feign
Feign這種技術(shù)應(yīng)用在服務(wù)消費(fèi)端
修改pom.xml配置文件,加入Feign的依賴包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
由于Fegin是將Rest的操作轉(zhuǎn)換成接口的形式,所以我們需要新建一個接口,并在接口上聲明@FeignClient注解
@FeignClient(value = "DEPT-PROVIDER",configuration = FeignClientConfig.class)
public interface DeptClientService {
@RequestMapping(method= RequestMethod.GET,value="/dept/get/{id}")
public Dept get(@PathVariable("id") long id) ;
@RequestMapping(method=RequestMethod.GET,value="/dept/list")
public List<Dept> list() ;
@RequestMapping(method=RequestMethod.POST,value="/dept/add")
public boolean add(Dept dept) ;
}
@Configuration
public class FeignClientConfig {
@Bean
public Logger.Level getFeignLoggerLevel() {
return feign.Logger.Level.FULL ;
}
@Bean
public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("admin", "admin");
}
}
其中configuration = FeignClientConfig.class不是必須的,將configuration屬性去除仍然能work。
將之前的Rest操作的API,替換成面向DeptClientService接口的形式
@RestController
@RequestMapping("/consumer/dept")
public class ConsumerDeptController {
@Autowired
private DeptClientService deptClientService;
@RequestMapping(value = "/get")
public Dept get(long id) {
return this.deptClientService.get(id);
}
@RequestMapping("/list")
public List<Dept> 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";
@Autowired
private RestTemplate restTemplate;
@Autowired
private HttpHeaders httpHeaders;
@Autowired
private LoadBalancerClient loadBalancerClient;
@RequestMapping(value = "/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<Object>(this.httpHeaders),Dept.class).getBody();
return dept;
}
@RequestMapping("/list")
public List<Dept> list(){
//List<Dept> deptList = restTemplate.getForObject(DEPT_LIST_URL, List.class);
List<Dept> deptList = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,new HttpEntity<Object>(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<Object>(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自帶了負(fù)責(zé)均衡特性,所以使用Feign之后可以不用使用Ribbon。
Feign的配置
Feign 最重要的功能就是將 Rest 服務(wù)的信息轉(zhuǎn)換為接口,但是在實(shí)際的使用之中也需要考慮到一些配置情況,例如:數(shù)據(jù)壓縮, Rest 的核心本質(zhì)在于: JSON 數(shù)據(jù)傳輸( XML、文本),于是就必須思考一種情況,萬一用戶發(fā)送的數(shù)據(jù)很大呢? 所以這個時候可以考慮修改application.yml 配置文件對傳輸數(shù)據(jù)進(jìn)行壓縮;
feign: compression: request: mime-types: # 可以被壓縮的類型 - text/xml - application/xml - application/json min-request-size: 2048 # 超過2048的字節(jié)進(jìn)行壓縮
開啟Feign的日志(默認(rèn)是不開啟的)
logging: level: cn.zgc.service: DEBUG
feign調(diào)用的時候返回參數(shù)不是期望的數(shù)據(jù)類型
今天碰到個坑,feign調(diào)用其他應(yīng)用的時候反參并不是期望的類型;不知道這個在其他請求方式有沒有這個bug
解決的方法
是在feign調(diào)用的那里指定Response<?>的泛型類,我這邊是碰到期望是返回Long類型,但是返回的是Integer類型,不知道是不是隱式轉(zhuǎn)換了,這里記錄一下這個Bug
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程使用同步鎖交替執(zhí)行打印奇數(shù)偶數(shù)的方法
這篇文章主要介紹了Java線程使用同步鎖交替執(zhí)行打印奇數(shù)偶數(shù)的方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
SpringBoot?jackson提供對LocalDate的支持方式
這篇文章主要介紹了SpringBoot?jackson提供對LocalDate的支持方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java中BigDecimal除法使用不當(dāng)導(dǎo)致精度問題
本文主要介紹了Java中BigDecimal除法使用不當(dāng)導(dǎo)致精度問題,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Java長度不足左位補(bǔ)0的3種實(shí)現(xiàn)方法
這篇文章主要介紹了Java長度不足左位補(bǔ)0的3種實(shí)現(xiàn)方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
深入淺出講解Spring框架中AOP及動態(tài)代理的應(yīng)用
在軟件業(yè),AOP為Aspect?Oriented?Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期間動態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)2022-03-03
SpringBoot全局Controller返回值格式統(tǒng)一
本文主要介紹了SpringBoot全局Controller返回值格式統(tǒng)一,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07

