参数为messageConverters的构造
/** * Create a new instance of the {@link RestTemplate} using the given list of * {@link HttpMessageConverter} to use * @param messageConverters the list of {@link HttpMessageConverter} to use * @since 3.2.7 * 传入自定义的HttpMessageConverter集合,并赋值给messageConverters,之后使用自定义的HttpMessageConverter */ public RestTemplate(List<HttpMessageConverter<?>> messageConverters) { Assert.notEmpty(messageConverters, "At least one HttpMessageConverter required"); this.messageConverters.addAll(messageConverters); } 三、RestTemplate API使用RestTemplate 4.3.9.RELEASE版本 API
HTTP Method RestTemplate MethodDELETE void delete(String url, Object... uriVariables)
void delete(String url, Map<String,?> uriVariables)
void delete(URI url)
GET T getForObject(String url, Class responseType, Object... uriVariables)
T getForObject(String url, Class responseType, Map<String,?> uriVariables)
T getForObject(URI url, Class responseType)
ResponseEntity getForEntity(String url, Class responseType, Object... uriVariables)
ResponseEntity getForEntity(String url, Class responseType, Map<String,?> uriVariables)
ResponseEntity getForEntity(URI url, Class responseType)
HEAD HttpHeaders headForHeaders(String url, Object... uriVariables)
HttpHeaders headForHeaders(String url, Map<String,?> uriVariables)
HttpHeaders headForHeaders(URI url)
OPTIONS Set optionsForAllow(String url, Object... uriVariables)
Set optionsForAllow(String url, Map<String,?> uriVariables)
Set optionsForAllow(URI url)
POST T postForObject(String url, Object request, Class responseType, Object... uriVariables)
T postForObject(String url, Object request, Class responseType, Map<String,?> uriVariables)
T postForObject(URI url, Object request, Class responseType)
ResponseEntity postForEntity(String url, Object request, Class responseType, Object... uriVariables)
ResponseEntity postForEntity(String url, Object request, Class responseType, Map<String,?> uriVariables)
ResponseEntity postForEntity(URI url, Object request, Class responseType)
PUT void put(String url, Object request, Object... uriVariables)
void put(String url, Object request, Map<String,?> uriVariables)
void put(URI url, Object request)
PATCH and others exchange()、execute()
RestTemplate的方法名遵循一定的命名规范,第一部分表示用哪种HTTP方法调用(get,post),第二部分表示返回类型
getForObject() -- 发送GET请求,将HTTP response转换成一个指定的object对象
postForEntity() -- 发送POST请求,将给定的对象封装到HTTP请求体,返回类型是一个HttpEntity对象
每个HTTP方法对应的RestTemplate方法都有3种。其中2种的url参数为字符串,URI参数变量分别是Object数组和Map,第3种使用URI类型作为参数
注意,使用字符串类型的url默认会对url进行转义,如 list在执行时会转义为%20list,这样其实是没有问题的,但如果字符串类型的url本身已经转义过了,执行时就会再转义一次,变成%2520list。如果不需要这种隐式的转义,可以使用java.net.URI参数的方法,这种方法不会在执行时存在隐式的url转义,可以在创建URI对象时自行决定是否转义,推荐使用UriComponentsBuilder创建URI
UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://example.com/hotels/{hotel}/bookings/{booking}") .build() //build(true)就不会对url转义,但如果包含 list这种需要转义的url,会报错 .expand("42", "21") .encode(); URI uri = uriComponents.toUri();exchange 和execute 方法比上面列出的其它方法(如getForObject、postForEntity等)使用范围更广,允许调用者指定HTTP请求的方法(GET、POST、PUT等),并且可以支持像HTTP PATCH(部分更新),但需要底层的HTTP库支持,JDK自带的HttpURLConnection不支持PATCH方法,Apache的HTTPClient 4.2及以后版本支持
GET方法 getForEntity()发送GET请求,返回ResponseEntity
/** * 参数1: String类型 或 URI类型的请求地址 * 参数2: 指定返回的实体类型,class对象 * 参数3: uri参数,可以是变长数组或map * 返回值:ResponseEntity<T>是Spring对HTTP响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、response header信息,response body信息等 */ @Override public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException { RequestCallback requestCallback = acceptHeaderRequestCallback(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); } @Override public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException { RequestCallback requestCallback = acceptHeaderRequestCallback(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); } @Override public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException { RequestCallback requestCallback = acceptHeaderRequestCallback(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.GET, requestCallback, responseExtractor); }