咱們能夠經過Spring Boot快速開發REST接口,同時也可能須要在實現接口的過程當中,經過Spring Boot調用內外部REST接口完成業務邏輯。html
在Spring Boot中,調用REST Api常見的通常主要有兩種方式,經過自帶的RestTemplate或者本身開發http客戶端工具實現服務調用。java
RestTemplate基本功能很是強大,不過某些特殊場景,咱們可能仍是更習慣用本身封裝的工具類,好比上傳文件至分佈式文件系統、處理帶證書的https請求等。git
本文以RestTemplate來舉例,記錄幾個使用RestTemplate調用接口過程當中發現的問題和解決方案。github
咱們本身封裝的HttpClient,一般都會有一些模板代碼,好比創建鏈接,構造請求頭和請求體,而後根據響應,解析響應信息,最後關閉鏈接。web
RestTemplate是Spring中對HttpClient的再次封裝,簡化了發起HTTP請求以及處理響應的過程,抽象層級更高,減小消費者的模板代碼,使冗餘代碼更少。spring
其實仔細想一想Spring Boot下的不少XXXTemplate類,它們也提供各類模板方法,只不過抽象的層次更高,隱藏了更多細節而已。json
順便提一下,Spring Cloud有一個聲明式服務調用Feign,是基於Netflix Feign實現的,整合了Spring Cloud Ribbon與 Spring Cloud Hystrix,而且實現了聲明式的Web服務客戶端定義方式。api
本質上Feign是在RestTemplate的基礎上對其再次封裝,由它來幫助咱們定義和實現依賴服務接口的定義。數組
常見的REST服務有不少種請求方式,如GET,POST,PUT,DELETE,HEAD,OPTIONS等。RestTemplate實現了最多見的方式,用的最多的就是Get和Post了,調用API可參考源碼,這裏列舉幾個方法定義(GET、POST、DELETE):app
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,Object... uriVariables) public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request,Class<T> responseType, Object... uriVariables) public void delete(String url, Object... uriVariables) public void delete(URI url)
同時要注意兩個較爲「靈活」的方法exchange和execute。
RestTemplate暴露的exchange與其它接口的不一樣:
(1)容許調用者指定HTTP請求的方法(GET,POST,DELETE等)
(2)能夠在請求中增長body以及頭信息,其內容經過參數‘HttpEntity<?>requestEntity’描述
(3)exchange支持‘含參數的類型’(即泛型類)做爲返回類型,該特性經過‘ParameterizedTypeReference<T>responseType’描述。
RestTemplate全部的GET,POST等等方法,最終調用的都是execute方法。excute方法的內部實現是將String格式的URI轉成了java.net.URI,以後調用了doExecute方法,doExecute方法的實現以下:
doExecute方法封裝了模板方法,好比建立鏈接、處理請求和應答,關閉鏈接等。
多數人看到這裏,估計都會以爲封裝一個RestClient不過如此吧?
以一個POST調用爲例:
package com.power.demo.restclient; import com.power.demo.common.AppConst; import com.power.demo.restclient.clientrequest.ClientGetGoodsByGoodsIdRequest; import com.power.demo.restclient.clientresponse.ClientGetGoodsByGoodsIdResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; /** * 商品REST接口客戶端 (demo測試用) **/ @Component public class GoodsServiceClient { //服務消費者調用的接口URL 形如:http://localhost:9090 @Value("${spring.power.serviceurl}") private String _serviceUrl; @Autowired private RestTemplate restTemplate; public ClientGetGoodsByGoodsIdResponse getGoodsByGoodsId(ClientGetGoodsByGoodsIdRequest request) { String svcUrl = getGoodsSvcUrl() + "/getinfobyid"; ClientGetGoodsByGoodsIdResponse response = null; try { response = restTemplate.postForObject(svcUrl, request, ClientGetGoodsByGoodsIdResponse.class); } catch (Exception e) { e.printStackTrace(); response = new ClientGetGoodsByGoodsIdResponse(); response.setCode(AppConst.FAIL); response.setMessage(e.toString()); } return response; } private String getGoodsSvcUrl() { String url = ""; if (_serviceUrl == null) { _serviceUrl = ""; } if (_serviceUrl.length() == 0) { return url; } if (_serviceUrl.substring(_serviceUrl.length() - 1, _serviceUrl.length()) == "/") { url = String.format("%sapi/v1/goods", _serviceUrl); } else { url = String.format("%s/api/v1/goods", _serviceUrl); } return url; } }
demo裏直接RestTemplate.postForObject方法調用,反序列化實體轉換這些RestTemplate內部封裝搞定。
這個問題一般會出如今postForObject中傳入對象進行調用的時候。
分析RestTemplate源碼,在HttpEntityRequestCallback類的doWithRequest方法中,若是messageConverters(這個字段後面會繼續說起)列表字段循環處理的過程當中沒有知足return跳出的邏輯(也就是沒有匹配的HttpMessageConverter),則拋出上述異常:
@Override @SuppressWarnings("unchecked") public void doWithRequest(ClientHttpRequest httpRequest) throws IOException { super.doWithRequest(httpRequest); Object requestBody = this.requestEntity.getBody(); if (requestBody == null) { HttpHeaders httpHeaders = httpRequest.getHeaders(); HttpHeaders requestHeaders = this.requestEntity.getHeaders(); if (!requestHeaders.isEmpty()) { for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) { httpHeaders.put(entry.getKey(), new LinkedList<>(entry.getValue())); } } if (httpHeaders.getContentLength() < 0) { httpHeaders.setContentLength(0L); } } else { Class<?> requestBodyClass = requestBody.getClass(); Type requestBodyType = (this.requestEntity instanceof RequestEntity ? ((RequestEntity<?>)this.requestEntity).getType() : requestBodyClass); HttpHeaders httpHeaders = httpRequest.getHeaders(); HttpHeaders requestHeaders = this.requestEntity.getHeaders(); MediaType requestContentType = requestHeaders.getContentType(); for (HttpMessageConverter<?> messageConverter : getMessageConverters()) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<Object> genericConverter = (GenericHttpMessageConverter<Object>) messageConverter; if (genericConverter.canWrite(requestBodyType, requestBodyClass, requestContentType)) { if (!requestHeaders.isEmpty()) { for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) { httpHeaders.put(entry.getKey(), new LinkedList<>(entry.getValue())); } } if (logger.isDebugEnabled()) { if (requestContentType != null) { logger.debug("Writing [" + requestBody + "] as \"" + requestContentType + "\" using [" + messageConverter + "]"); } else { logger.debug("Writing [" + requestBody + "] using [" + messageConverter + "]"); } } genericConverter.write(requestBody, requestBodyType, requestContentType, httpRequest); return; } } else if (messageConverter.canWrite(requestBodyClass, requestContentType)) { if (!requestHeaders.isEmpty()) { for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) { httpHeaders.put(entry.getKey(), new LinkedList<>(entry.getValue())); } } if (logger.isDebugEnabled()) { if (requestContentType != null) { logger.debug("Writing [" + requestBody + "] as \"" + requestContentType + "\" using [" + messageConverter + "]"); } else { logger.debug("Writing [" + requestBody + "] using [" + messageConverter + "]"); } } ((HttpMessageConverter<Object>) messageConverter).write( requestBody, requestContentType, httpRequest); return; } } String message = "Could not write request: no suitable HttpMessageConverter found for request type [" + requestBodyClass.getName() + "]"; if (requestContentType != null) { message += " and content type [" + requestContentType + "]"; } throw new RestClientException(message); } }
最簡單的解決方案是,能夠經過包裝http請求頭,並將請求對象序列化成字符串的形式傳參,參考示例代碼以下:
若是咱們還想直接返回對象,直接反序列化返回的字符串便可:
/* * Post請求調用 * */ public static <T> T postForObject(RestTemplate restTemplate, String url, Object params, Class<T> clazz) { T response = null; String respStr = postForObject(restTemplate, url, params); response = SerializeUtil.DeSerialize(respStr, clazz); return response; }
其中,序列化和反序列化工具比較多,經常使用的好比fastjson、jackson和gson。
和發起請求發生異常同樣,處理應答的時候也會有問題。
StackOverflow上有人問過相同的問題,根本緣由是HTTP消息轉換器HttpMessageConverter缺乏MIME Type,也就是說HTTP在把輸出結果傳送到客戶端的時候,客戶端必須啓動適當的應用程序來處理這個輸出文檔,這能夠經過多種MIME(多功能網際郵件擴充協議)Type來完成。
對於服務端應答,不少HttpMessageConverter默認支持的媒體類型(MIMEType)都不一樣。StringHttpMessageConverter默認支持的則是MediaType.TEXT_PLAIN,SourceHttpMessageConverter默認支持的則是MediaType.TEXT_XML,FormHttpMessageConverter默認支持的是MediaType.APPLICATION_FORM_URLENCODED和MediaType.MULTIPART_FORM_DATA,在REST服務中,咱們用到的最多的仍是MappingJackson2HttpMessageConverter,這是一個比較通用的轉化器(繼承自GenericHttpMessageConverter接口),根據分析,它默認支持的MIMEType爲MediaType.APPLICATION_JSON:
可是有些應用接口默認的應答MIMEType不是application/json,好比咱們調用一個外部天氣預報接口,若是使用RestTemplate的默認配置,直接返回一個字符串應答是沒有問題的:
String url = "http://wthrcdn.etouch.cn/weather_mini?city=上海"; String result = restTemplate.getForObject(url, String.class); ClientWeatherResultVO vo = SerializeUtil.DeSerialize(result, ClientWeatherResultVO.class);
可是,若是咱們想直接返回一個實體對象:
String url = "http://wthrcdn.etouch.cn/weather_mini?city=上海"; ClientWeatherResultVO weatherResultVO = restTemplate.getForObject(url, ClientWeatherResultVO.class);
則直接報異常:
Could not extract response: no suitable HttpMessageConverter found for response type [class ]
and content type [application/octet-stream]
不少人碰到過這個問題,首次碰到估計大多都比較懵吧,不少接口都是json或者xml或者plain text格式返回的,什麼是application/octet-stream?
查看RestTemplate源代碼,一路跟蹤下去會發現HttpMessageConverterExtractor類的extractData方法有個解析應答及反序列化邏輯,若是不成功,拋出的異常信息和上述一致:
@Override @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public T extractData(ClientHttpResponse response) throws IOException { MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response); if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) { return null; } MediaType contentType = getContentType(responseWrapper); try { for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter; if (genericMessageConverter.canRead(this.responseType, null, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + this.responseType + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return (T) genericMessageConverter.read(this.responseType, null, responseWrapper); } } if (this.responseClass != null) { if (messageConverter.canRead(this.responseClass, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + this.responseClass.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]"); } return (T) messageConverter.read((Class) this.responseClass, responseWrapper); } } } } catch (IOException | HttpMessageNotReadableException ex) { throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", ex); } throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]"); }
StackOverflow上的解決的示例代碼能夠接受,可是並不許確,常見的MIMEType都應該加進去,貼一下我認爲正確的代碼:
package com.power.demo.restclient.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.http.converter.*; import org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter; import org.springframework.http.converter.feed.AtomFeedHttpMessageConverter; import org.springframework.http.converter.feed.RssChannelHttpMessageConverter; import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.http.converter.json.JsonbHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter; import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter; import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; import org.springframework.http.converter.xml.SourceHttpMessageConverter; import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; import org.springframework.web.client.RestTemplate; import java.util.Arrays; import java.util.List; @Component public class RestTemplateConfig { private static final boolean romePresent = ClassUtils.isPresent("com.rometools.rome.feed.WireFeed", RestTemplate .class.getClassLoader()); private static final boolean jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder", RestTemplate.class.getClassLoader()); private static final boolean jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", RestTemplate.class.getClassLoader()) && ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", RestTemplate.class.getClassLoader()); private static final boolean jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", RestTemplate.class.getClassLoader()); private static final boolean jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", RestTemplate.class.getClassLoader()); private static final boolean jackson2CborPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.cbor.CBORFactory", RestTemplate.class.getClassLoader()); private static final boolean gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", RestTemplate.class.getClassLoader()); private static final boolean jsonbPresent = ClassUtils.isPresent("javax.json.bind.Jsonb", RestTemplate.class.getClassLoader()); // 啓動的時候要注意,因爲咱們在服務中注入了RestTemplate,因此啓動的時候須要實例化該類的一個實例 @Autowired private RestTemplateBuilder builder; @Autowired private ObjectMapper objectMapper; // 使用RestTemplateBuilder來實例化RestTemplate對象,spring默認已經注入了RestTemplateBuilder實例 @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = builder.build(); List<HttpMessageConverter<?>> messageConverters = Lists.newArrayList(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(objectMapper); //不加會出現異常 //Could not extract response: no suitable HttpMessageConverter found for response type [class ] MediaType[] mediaTypes = new MediaType[]{ MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON_UTF8, MediaType.TEXT_HTML, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, MediaType.APPLICATION_STREAM_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_PDF, }; converter.setSupportedMediaTypes(Arrays.asList(mediaTypes)); //messageConverters.add(converter); if (jackson2Present) { messageConverters.add(converter); } else if (gsonPresent) { messageConverters.add(new GsonHttpMessageConverter()); } else if (jsonbPresent) { messageConverters.add(new JsonbHttpMessageConverter()); } messageConverters.add(new FormHttpMessageConverter()); messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(new StringHttpMessageConverter()); messageConverters.add(new ResourceHttpMessageConverter(false)); messageConverters.add(new SourceHttpMessageConverter()); messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { messageConverters.add(new AtomFeedHttpMessageConverter()); messageConverters.add(new RssChannelHttpMessageConverter()); } if (jackson2XmlPresent) { messageConverters.add(new MappingJackson2XmlHttpMessageConverter()); } else if (jaxb2Present) { messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2SmilePresent) { messageConverters.add(new MappingJackson2SmileHttpMessageConverter()); } if (jackson2CborPresent) { messageConverters.add(new MappingJackson2CborHttpMessageConverter()); } restTemplate.setMessageConverters(messageConverters); return restTemplate; } }
看到上面的代碼,再對比一下RestTemplate內部實現,就知道我參考了RestTemplate的源碼,有潔癖的人可能會說這一坨代碼有點囉嗦,上面那一堆static final的變量和messageConverters填充數據方法,暴露了RestTemplate的實現,若是RestTemplate修改了,這裏也要改,很是不友好,並且看上去一點也不OO。
通過分析,RestTemplateBuilder.build()構造了RestTemplate對象,只要將內部MappingJackson2HttpMessageConverter修改一下支持的MediaType便可,RestTemplate的messageConverters字段雖然是private final的,咱們依然能夠經過反射修改之,改進後的代碼以下:
package com.power.demo.restclient.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @Component public class RestTemplateConfig { // 啓動的時候要注意,因爲咱們在服務中注入了RestTemplate,因此啓動的時候須要實例化該類的一個實例 @Autowired private RestTemplateBuilder builder; @Autowired private ObjectMapper objectMapper; // 使用RestTemplateBuilder來實例化RestTemplate對象,spring默認已經注入了RestTemplateBuilder實例 @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = builder.build(); List<HttpMessageConverter<?>> messageConverters = Lists.newArrayList(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(objectMapper); //不加可能會出現異常 //Could not extract response: no suitable HttpMessageConverter found for response type [class ] MediaType[] mediaTypes = new MediaType[]{ MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM, MediaType.TEXT_HTML, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, MediaType.APPLICATION_STREAM_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_PDF, }; converter.setSupportedMediaTypes(Arrays.asList(mediaTypes)); try { //經過反射設置MessageConverters Field field = restTemplate.getClass().getDeclaredField("messageConverters"); field.setAccessible(true); List<HttpMessageConverter<?>> orgConverterList = (List<HttpMessageConverter<?>>) field.get(restTemplate); Optional<HttpMessageConverter<?>> opConverter = orgConverterList.stream() .filter(x -> x.getClass().getName().equalsIgnoreCase(MappingJackson2HttpMessageConverter.class .getName())) .findFirst(); if (opConverter.isPresent() == false) { return restTemplate; } messageConverters.add(converter);//添加MappingJackson2HttpMessageConverter //添加原有的剩餘的HttpMessageConverter List<HttpMessageConverter<?>> leftConverters = orgConverterList.stream() .filter(x -> x.getClass().getName().equalsIgnoreCase(MappingJackson2HttpMessageConverter.class .getName()) == false) .collect(Collectors.toList()); messageConverters.addAll(leftConverters); System.out.println(String.format("【HttpMessageConverter】原有數量:%s,從新構造後數量:%s" , orgConverterList.size(), messageConverters.size())); } catch (Exception e) { e.printStackTrace(); } restTemplate.setMessageConverters(messageConverters); return restTemplate; } }
除了一個messageConverters字段,看上去咱們再也不關心RestTemplate那些外部依賴包和內部構造過程,果真乾淨簡潔好維護了不少。
這個也是一個很是經典的問題。解決方案很是簡單,找到HttpMessageConverter,看看默認支持的Charset。AbstractJackson2HttpMessageConverter是不少HttpMessageConverter的基類,默認編碼爲UTF-8:
而StringHttpMessageConverter比較特殊,有人反饋過發生亂碼問題由它默認支持的編碼ISO-8859-1引發:
若是在使用過程當中發生亂碼,咱們能夠經過方法設置HttpMessageConverter支持的編碼,經常使用的有UTF-八、GBK等。
這是開發過程當中容易碰到的又一個問題。由於Java的開源框架和工具類很是之多,並且版本更迭頻繁,因此常常發生一些意想不到的坑。
以joda time爲例,joda time是流行的java時間和日期框架,可是若是你的接口對外暴露joda time的類型,好比DateTime,那麼接口調用方(同構和異構系統)可能會碰到序列化難題,反序列化時甚至直接拋出以下異常:
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.joda.time.Chronology]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.joda.time.Chronology` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (PushbackInputStream);
我在前廠就碰到過,能夠參考這裏,後來爲了調用方便,改回直接暴露Java的Date類型。
固然解決的方案不止這一種,可使用jackson支持自定義類的序列化和反序列化的方式。在精度要求不是很高的系統裏,實現簡單的DateTime自定義序列化:
以及DateTime反序列化:
最後能夠在RestTemplateConfig類中對常見調用問題進行彙總處理,能夠參考以下:
目前良好地解決了RestTemplate常見調用問題,並且不須要你寫RestTemplate幫助工具類了。
上面列舉的這些常見問題,其實.NET下面也有,有興趣你們能夠搜索一下微軟的HttpClient常見使用問題,用過的人都深有體會。更不用提RestSharp這個開源類庫,幾年前用的過程當中發現了很是多的Bug,到如今還有一個反序列化數組的問題困擾着咱們,我只好本身造個簡單輪子特殊處理,給我最深入的經驗就是,不少看上去簡單的功能,真的碰到了依然會花掉很多的時間去排查和解決,甚至要翻看源碼。因此,咱們寫代碼要認識到,越是通用的工具,越須要考慮到特例,可能你須要花80%以上的精力去處理20%的特殊狀況,這估計也是知足常見的二八定律吧。
參考: