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

Just to review

In the last few articles, we looked at the various request type methods provided by RestTemplate and believe that we now have a basic understanding, but the one we didn’t cover is “multiple optional parameters”. What are the parameters? How to choose?

Starting from this article, we will start to learn specific methods to use, ready to make a little progress every day.

Request method parameter analysis

A Get request

General parameters:

Url: Access link URL, nothing to say.

ResponseType: Specifies the type of the response parameter to return. For example, if the response parameter is a List, this parameter should be passed to list.class.

getForObject

1.public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)

Object… UriVariables, by its name, are the URL parameters of the access request, and Object… , then naturally can be transmitted multiple, use the following code can be:

List<Map<String,String>> list = restTemplate.getForObject(url, List.class,"first param"."two param");
Copy the code

2.public

T getForObject(String url, Class

responseType, Map

uriVariables)
,>

Map<String,? < span style = “box-sizing: border-box; color: RGB (74, 74, 74); line-height: 22px; font-size: 14px! Important; word-break: inherit! Important;”

Map<String,String> map = new HashMap<>;
map.put("Frist"."first param");
map.put("Two"."two param");
List<Map<String,String>> list = restTemplate.getForObject(url, List.class, map);
Copy the code

3.public <T> T getForObject(URI url, Class<T> responseType)

This method does not need to be said, just pass the URL and the corresponding return parameter type.

getForEntity

For getForEntiy, there is no difference except for the argument that is returned, so without too much explanation, let’s give a code example to learn for yourself.

1.public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)

ResponseEntity<List> entity = restTemplate.getForEntity(url, List.class,"first param"."two param");
System.out.println(entity.getBody());
Copy the code

2.public

ResponseEntity

getForEntity(String url, Class

responseType, Map

uriVariables)
,>


Map<String,String> map = new HashMap<>;
map.put("Frist"."first param");
map.put("Two"."two param");
ResponseEntity<List> entity = restTemplate.getForEntity(url, List.class, map);
System.out.println(entity.getBody());
Copy the code

3.public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType)

summary

Today we learned how to use Get request methods. Did you learn anything?