In this paper, a personal blog address: www.leafage.top/posts/detai…

The GET:

GetForEntity (), getForObject();

1. GetForEntity () function:

This method returns ResponseEntity, which is Spring’s wrapper for the HTTP request response.

  • 1.1 getForEntity(String URL, Class responseType, Map urlVariables). When using this method to bind parameters, you need to specify the key value of the parameter in the Map in the placeholder. Example:
Map<String, Object> param = new HashMap<>();
param.put("name"."admin");
restTemplate.getForEntity("http://user-service/user/one? name={name}",String.class,param);
Copy the code

After this method call, “admin” is replaced with a placeholder {1}.

  • GetForEntity (String URL, Class responseType, Object… urlVariables)

Example:

restTemplate.getForEntity("http://user-service/user/one? name={1}",String.class,"admin");
Copy the code

After this method call, “admin” is replaced with a placeholder {1}.

  • 1.3 getForEntity(Uri url, Class responseType)

Example:

Uri uri = UriComponentBuilder.fromUriString("http://user-service/user/one? name={1}").build.expand("admin").encode().toUri();
restTemplate.getForEntity(uri,String.class).getBody();
Copy the code

2. GetForObject () function:

This method can be understood as the getForEntity further encapsulation, it through HttpMessageConverterExtractor object, the content of the HTTP request and response body body transformation, realize the request directly return packaging good object content. In the getForObject(Uri URL, Class responseType) method, you do not need to call the getBody() method.

POST:

PostForEntity (), postForObject(), postForLocation()

1. PostForEntity () function and postForObject() function:

GET (); GET (); GET ();

postForEntity(String url, Object request, Class responseType, Object... uriVariables) postForEntity(String url, Object request, Class responseType, Map uriVariables) postForEntity(URI url, Object Request, Class responseTypeCopy the code

Note: The postForObject() function is similar and will not be described here. Note the new request parameter, which can be a normal object or an HttpEntity object. If it is a normal Object instead of an HttpEntity Object, RestTemplate converts the request Object to an HttpEntity Object. Object is the type of the request, and the request content is treated as a complete body. If the Request is an HttpEntity object, it is treated as a completed HTTP request object that contains both the body and header contents. Example:

ResponseEntity<String> response = restTemplate.postForEntity("http://user-service/user", user, String.class);
String body = response.getBody();
Copy the code

2. PostForLocation () function

This method implements a POST request to submit a resource and returns the URI of the new resource. In addition, Class responseType is missing as postForEntity() and postForObject() :

postForLocation(Stringurl, Object request, Object... PostForLocation (URI URL, Object Request) postForLocation(String URL, Object Request)Copy the code

Use examples;

URI uri = restTemplate.postForLocation("http://user-service/user", user);
Copy the code

PUT:

PUT () is overloaded in three ways. Unlike GET and POST, PUT does not return a value, so there is no Class responseType:

put(String url, Object request, Object... urlVariables)
put(String url, Object request, Map urlVariables)
put(URI url, Object request)
Copy the code

DELETE:

A DELETE call to restTemplate: The delete() function usually concatenates the unique identifier of the delete request in the URL. The delete request does not require the body information of the request. The delete request is very similar to GET, except that it does not return a value:

delete(String url, Object ... urlVariables)
delete(String url, Map urlVariables)
delete(URI url)
Copy the code