RestTemplate中常见方法简介说明
下文笔者讲述RestTemplate方法的简介说明,如下所示
RestTemplate方法的简介
Spring中使用Rest资源
借助这个可以轻松访问资源
大多是与Http交互的方法
getForEntity():
发送一个HTTP GET请求
返回的ResponseEntity包含响应体所映射成的对象
getForObject():
发送一个HTTP GET请求
返回请求体将映射为一个对象
postForEntity()
POST数据到一个URL 返回包含一个对象ResponseEntity 这个对象是从响应体中映射
postForObject()
POST数据到一个URL 返回根据响应体匹配形成的对象
GET请求
getForEntity
getForEntity方法:
返回值是一个ResponseEntity<T>
ResponseEntity<T>:
是Spring对HTTP请求响应的封装
包括几个重要的元素
如:
响应码、contentType、contentLength、响应消息体等
@RequestMapping("/gethello")
public String getHello() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class);
String body = responseEntity.getBody();
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders headers = responseEntity.getHeaders();
StringBuffer result = new StringBuffer();
result.append("responseEntity.getBody():").append(body).append("<hr>")
.append("responseEntity.getStatusCode():").append(statusCode).append("<hr>")
.append("responseEntity.getStatusCodeValue():").append(statusCodeValue).append("<hr>")
.append("responseEntity.getHeaders():").append(headers).append("<hr>");
return result.toString();
}
getForEntity:
第一个参数为我要调用的服务的地址
getForEntity:
第二个参数String.class
返回body类型是String
例
@RequestMapping("/sayhello")
public String sayHello() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={1}", String.class, "猫猫");
return responseEntity.getBody();
}
@RequestMapping("/sayhello2")
public String sayHello2() {
Map<String, String> map = new HashMap<>();
map.put("name", "测试");
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={name}", String.class, map);
return responseEntity.getBody();
}
@RequestMapping("/sayhello3")
public String sayHello3() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://HELLO-SERVICE/sayhello?name={name}").build().expand("王五").encode();
URI uri = uriComponents.toUri();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri, String.class);
return responseEntity.getBody();
}
@RequestMapping(value = "/getbook1", method = RequestMethod.GET)
public Book book1() {
return new Book("马小跳", 22, "测试", "中信出版社");
}
//调用
@RequestMapping("/book1")
public Book book1() {
ResponseEntity<Book> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/getbook1", Book.class);
return responseEntity.getBody();
}
getForObject
getForObject函数: 是对getForEntity函数的进一步封装
@RequestMapping("/book2")
public Book book2() {
Book book = restTemplate.getForObject("http://HELLO-SERVICE/getbook1", Book.class);
return book;
}
POST请求
在RestTemplate中,POST请求可以通过如下三个方法来发起:第一种:postForEntity
该方法和get请求中的getForEntity方法类似,如下例子:
@RequestMapping("/book3")
public Book book3() {
Book book = new Book();
book.setName("马小跳");
ResponseEntity<Book> responseEntity = restTemplate.postForEntity("http://HELLO-SERVICE/getbook2", book, Book.class);
return responseEntity.getBody();
}
@RequestMapping(value = "/getbook2", method = RequestMethod.POST)
public Book book2(@RequestBody Book book) {
System.out.println(book.getName());
book.setPrice(16);
book.setAuthor("马小跳");
book.setPublisher("中信出版社");
return book;
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


