举例:
//参数是Book类型,返回值是ResponseEntity<Book>类型 ResponseEntity<Book> responseEntity = restTemplate.postForEntity("http://127.0.0.1:8080/updateBook", book, Book.class); Book book = responseEntity.getBody(); //响应体转换为Book类型 int statusCodeValue = responseEntity.getStatusCodeValue(); //响应状态码 HttpHeaders headers = responseEntity.getHeaders(); //响应头信息 postForObject()发送POST请求,返回指定的Object类型
/** * 参数1: String类型 或 URI类型的请求地址 * 参数2: 请求body,可以是HttpEntity类型(可设置request header),或其它Object类型 * 参数3: 指定返回的实体类型,class对象 * 参数4: uri参数,可以是变长数组或map * 返回值:responseType指定的Object类型 */ @Override public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException { RequestCallback requestCallback = httpEntityCallback(request, responseType); HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger); return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables); } @Override public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException { RequestCallback requestCallback = httpEntityCallback(request, responseType); HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger); return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables); } @Override public <T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException { RequestCallback requestCallback = httpEntityCallback(request, responseType); HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters()); return execute(url, HttpMethod.POST, requestCallback, responseExtractor); }举例:
//参数是Book类型,返回值也是Book类型 Book book = restTemplate.postForObject("http://127.0.0.1:8080/updatebook", book, Book.class); exchange方法可以支持多种HTTP方法,在参数中指定
可以在请求中增加header和body信息,返回类型是ResponseEntity,可以从中获取响应的状态码,header和body等信息
HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyRequestHeader", "MyValue"); HttpEntity requestEntity = new HttpEntity(requestHeaders); HttpEntity<String> response = template.exchange( "http://example.com/hotels/{hotel}", HttpMethod.GET, //GET请求 requestEntity, //requestEntity,可以设置请求header、body String.class, "42"); String responseHeader = response.getHeaders().getFirst("MyResponseHeader"); //响应头信息 String body = response.getBody(); 四、RestTemplate扩展/配置 1、处理请求头和响应头 设置请求头信息(1)如果是发送post、put请求,要设置请求头,可以在调用方法时的第二个参数传入HttpEntity对象,HttpEntity可以用于设置请求头信息,如
HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyRequestHeader", "MyValue"); HttpEntity requestEntity = new HttpEntity(requestHeaders); Book book = restTemplate.postForObject("http://127.0.0.1:8080/getbook", requestEntity, Book.class);以postForObject()方法举例,其第二个参数接收Object类型的数据,如传入的是HttpEntity,则使用它作为整个请求实体,如果传入的是其它Object类型,则将Object参数作为request body,新建一个HttpEntity作为请求实体
private HttpEntityRequestCallback(Object requestBody, Type responseType) { super(responseType); //如果是HttpEntity类型的,直接作为请求实体赋值给this.requestEntity if (requestBody instanceof HttpEntity) { this.requestEntity = (HttpEntity<?>) requestBody; } //如果requestBody不是HttpEntity类型,且不为空,以Object参数作为request body,并新建HttpEntity else if (requestBody != null) { this.requestEntity = new HttpEntity<Object>(requestBody); } else { this.requestEntity = HttpEntity.EMPTY; } }(2)如果是其它HTTP方法调用要设置请求头,可以使用exchange()方法,可以参考
HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyRequestHeader", "MyValue"); HttpEntity requestEntity = new HttpEntity(requestHeaders); HttpEntity<String> response = template.exchange( "http://example.com/hotels/{hotel}", HttpMethod.GET, requestEntity, String.class, "42"); String responseHeader = response.getHeaders().getFirst("MyResponseHeader"); String body = response.getBody();总之,设置request header信息,需要找到对应的restTemplate方法中可以使用HttpEntity作为参数的,提前设置好请求头信息