0%

详解 RESTTEMPLATE 操作

RestTemplate定義了36個Rest資源交互的方法,其中的大多數都對應于http的方法。
delete() 在特定的URL上对资源执行HTTP DELETE操作

exchange()
在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中
映射得到的

execute() 在URL上执行特定的HTTP方法,返回一个从响应体映射得到的对象

getForEntity() 发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象

getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象

postForEntity()
POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得
到的

postForObject() POST 数据到一个URL,返回根据响应体匹配形成的对象

headForHeaders() 发送HTTP HEAD请求,返回包含特定资源URL的HTTP头

optionsForAllow() 发送HTTP OPTIONS请求,返回对特定URL的Allow头信息

postForLocation() POST 数据到一个URL,返回新创建资源的URL

put() PUT 资源到特定的URL

getForEntity說明:
基本的形式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @author itguang
* @create 2017-12-17 10:37
**/
@RestController
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(value = “getAll”)
public List<UserEntity> getUser() {
List<UserEntity> list = userService.getAll();
return list;
}

@RequestMapping(“get/{id}”)
public UserEntity getById(@PathVariable(name = “id”) String id) {

return userService.getById(id);
}
@RequestMapping(“get/{id}/{type}”)
public UserEntity getByIdAndType(@PathVariable(name = “id”) String id,String type) {

return userService.getByIdAndType(id,type);
}

@RequestMapping(value = “save”)
public String save(UserEntity userEntity) {

return “保存成功”;
}


@RequestMapping(value = “saveByType/{type}”)
public String saveByType(UserEntity userEntity,@PathVariable(“type”)String type) {

return “保存成功,type=”+type;
}


}

改變的:

1
2
3
4
5
6
7
8
9
10
11
12
@RequesyMapping(“getForEntity”)
public List<UserEntity> getAll2(){
ResponseEntity<List> responseEntity = restTemplate.getForEntity(“http://localhost/getAll”,List.class);
HttpHeaders headers = responEntity.getHeaders();
HttpStatus statusCode = responseEntity.getStatusCode();
int code = statusCode.value();

List<UserEntity> list = responseEntity.getBody();

System.out.println(list.toString());
return list;
}

有參數的:

1
2
3
4
5
6
7
@RequestMapping(“getForEntity/{id}”)
public UserEntity getById(@PathVariable(name = “id”) String id) {

ResponseEntity<UserEntity> responseEntity = restTemplate.getForEntity(“http://localhost/get/{id}”, UserEntity.class, id);
UserEntity userEntity = responseEntity.getBody();
return userEntity;
}

參數多的情況:

1
2
3
4
5
6
7
8
9
@RequestMapping(“getForEntity/{id}/{type}”)
public UserEntity getById(@PathVariable(name = “id”) String id,@PathVariable(name = “type”) Stringtype) {
Map map = new HashMap();
map.put(“id”,id);
map.put(“type”, type);
ResponseEntity<UserEntity> responseEntity = restTemplate.getForEntity(“http://localhost/get/{id}/{type}”, UserEntity.class,map);
UserEntity userEntity = responseEntity.getBody();
return userEntity;
}

getForObject 和 getForEntity 用法几乎相同,指示返回值返回的是 响应体,省去了我们 再去 getBody() .getForObject都是service是什麼類型,就是獲取的是什麼類型。
測試getForObject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public List<UserEntity> getAll(){
List<UserEntity> list=restTemplate.getForObject(“http://localhost/getAll”,List.class);
System.out.println(list.toString());
return list;
}
// 有参数的 postForEntity 请求,使用map封装
@RequestMapping(“saveUserByType2/{type}”)
public String save3(UserEntity userEntity,@PathVariable(“type”)String type) {
HashMap<String, String> map = new HashMap<>();
map.put(“type”, type);

ResponseEntity<String> responseEntity = restTemplate.postForEntity(“http://localhost/saveByType/{type}”, userEntity, String.class,map);
String body = responseEntity.getBody();

return body;

}