Articles Catalogue
Introduction to RestTemplate
1. Overview of RestTemplate
2. RestTemplate Configuration Items
- setBufferRequestBody
Whether or not to buffer the stream to store the request body, default true - setProxy
Setting proxy objects - setChunkSize
Set the byte length for each transfer, in conjunction with setBufferRequestBody (false) - setConnectTimeout
Set connection timeout, default - 1 - setReadTimeout
Set read content timeout, default - 1 - setOutputStreaming
Set Connection to Set Output Flow - setTaskExecutor
Setting up asynchronous callback actuator
3. RestTemplate request mode
- GET
Get resources by requesting URI s - POST
Used to add new content - PUT
Used to modify something, add if it does not exist - DELETE
Delete something - OPTIONS
Ask what methods can be implemented - HEAD
Similar to GET, but without returning body information, it checks whether an object exists and obtains its metadata. - CONNECT
Used for proxy transmission, such as using SSL - TRACE
Used for remote diagnostic server
II. RestTemplate use
1. Get request mode
- getForEntity()
Send an HTTP GET request, and the ResponseEntity returned contains the object mapped by the response body. - getForObject()
Send an HTTP GET request, and the returned body of the request will be mapped to an object
(1) Requesting Party
@RestController public class UserRibbonController { @Resource private RestTemplate restTemplate; //No parameters @GetMapping("/user") public Result select() throws Exception{ return restTemplate.getForObject("http://eureka-client1/select/", Result.class); } //Single parameter @GetMapping("/selectById/{uid}") public Result selectById(@PathVariable("uid") int uid) throws Exception{ return restTemplate.getForObject("http://eureka-client1/selectById/" + uid, Result.class); } //Multiple parameters @GetMapping("/updateById") public Result updateById(Users users) throws Exception{ return restTemplate.getForObject("http://eureka-client1/updateById/" + users.getPassword() +"/"+ users.getUid(), Result.class); } }
(2) Receiver
@RestController public class UserClientController { @Resource private UserService us; //No parameters @GetMapping("/select") public Result select() throws Exception{ Result result = new Result(); List<Users> list = us.select(); if(list.size() > 0) { result.setList(list); result.setCode("success"); }else { result.setMessage("error"); } return result; } //Single parameter @GetMapping("/selectById/{uid}") public Result selectById(@PathVariable("uid") int uid) throws Exception{ Result result = new Result(); Users users = us.selectById(uid); if(users != null) { result.setObject(users); result.setCode("success"); }else { result.setMessage("error"); } return result; } //Multiple parameters @GetMapping("/updateById/{password}/{uid}") public Result updateById(@PathVariable("password") String password, @PathVariable("uid") int uid) throws Exception{ Users users = new Users(); users.setUid(uid); users.setPassword(password); Result result = new Result(); us.updateById(users); result.setCode("success"); return result; } }
2. Post request mode
- postForEntity()
POST data goes to a URL and returns ResponseEntity containing an object that is mapped from the body of the response.
Here we are. - postForObject()
POST data goes to a URL and returns the object formed by matching the response volume. - postForLocation()
POST data goes to a URL and returns the URL of the newly created resource
(1) Requesting Party
@RestController public class UserClientController { @Resource private RestTemplate restTemplate; @GetMapping("/updateById") public Result updateById(Users users) throws Exception{ Result result = restTemplate.postForObject("http://eureka-client1/updateById", users, Result.class); return result; } }
(2) Receiver
@RestController public class UserClientController { @Resource private UserService us; @PostMapping("updateById") public Result updateById(@RequestBody Users users) throws Exception{ System.out.println(users.getUsername()); Result result = new Result(); us.updateById(users); result.setCode("success"); return result; } }
3. Put request mode
- put()
PUT resource to a specific URL, put request mode does not return parameters
(1) Requesting Party
@RestController public class UserRibbonController { @Resource private RestTemplate restTemplate; @GetMapping("/updateById") public Result updateById(Users users) throws Exception{ restTemplate.put("http://eureka-client1/updateById", users, 11); Result result = new Result(); result.setCode("success"); return result; } }
(2) Receiver
@RestController public class UserClientController { @Resource private UserService us; @PutMapping("updateById") public void updateById(@RequestBody Users users) throws Exception{ us.updateById(users); } }
3. Delete request
- delete()
Perform HTTP DELETE operations on resources at specific URL s
4. RestTemplate other methods
- exchange()
Execute a specific HTTP method on the URL and return the ResponseEntity containing the object from the body of the response
Mapped - execute()
Executing a specific HTTP method on the URL returns an object mapped from the response volume - headForHeaders()
Send an HTTP HEAD request and return the HTTP header containing a specific resource URL - optionsForAllow()
Send an HTTP OPTIONS request, returning the Allow header information for a specific URL