Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

In spring-Boot development, RestTemplate also provides an external access interface API, which mainly introduces the use of Get and Post methods. The Get request provides two types of interfaces getForObject and getForEntity. GetForEntity provides the implementation of the following three methods.

GetForEntity (Stringurl,Class responseType,Object… urlVariables)

ResponseType specifies the wrapper type of the body of the request response. UrlVariables specifies the parameter binding in the URL. Reference calls to this method are as follows:

// http://USER-SERVICE/user? name={name) RestTemplate restTemplate=new RestTemplate(); Map<String,String> params=new HashMap<>(); params.put("name","dada"); // ResponseEntity<String> responseEntity=restTemplate.getForEntity("http://USERSERVICE/user? name={name}",String.class,params);Copy the code

Get request — getForEntity(URI URL,Class responseType)

The method uses a URI object instead of the PREVIOUS URL and urlVariables parameters to specify access addresses and parameter bindings. A URI is a class in the JDK Java.net package that represents a Uniform Resource Identifier reference. For reference:

RestTemplate restTemplate=new RestTemplate();
UriComponents uriComponents=UriComponentsBuilder.fromUriString("http://USER-SERVICE/user?name={name}")
    .build()
    .expand("dodo")
    .encode();
URI uri=uriComponents.toUri();
ResponseEntity<String> responseEntity=restTemplate.getForEntity(uri,String.class).getBody();
Copy the code

Get request — getForObject

GetForObject 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. The getForObject methods are as follows:

getForObject(String url,Class responseType,Object... urlVariables) getForObject(String url,Class responseType,Map urlVariables) getForObject(URI url,Class responseType)Copy the code

A Post request

Post requests are provided in three ways, postForEntity, postForObject, and postForLocation. There are three methods for each method, postForEntity method is used as follows:

RestTemplate restTemplate=new RestTemplate(); User user=newUser("didi",30); ResponseEntity<String> responseEntity=restTemplate.postForEntity("http://USER-SERVICE/user",user,String.class); String body=responseEntity.getBody(); responseEntity.getBody();Copy the code

PostForEntity has the following three overloaded methods

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

The other arguments in postForEntity are pretty much the same as the arguments in getForEntity and I’m not going to go into that.