In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to use RestTemplate GET requests. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. GetForObject () method 1.1. Accept request result data in the way of String
Write a unit test case in the Spring Boot environment and receive the response result information as a String type
@ SpringBootTestclass ResttemplateWithSpringApplicationTests {@ Resource private RestTemplate restTemplate; @ Test void testSimple () {String url = "http://jsonplaceholder.typicode.com/posts/1"; String str = restTemplate.getForObject (url, String.class); System.out.println (str);}}
The second parameter of getForObject is the type of return value, and String.class accepts the getForObject response result as a string.
1.2. Accept the result data as a POJO object
Write a unit test case in the Spring Boot environment and receive the response result information with the java POJO object
@ Testpublic void testPoJO () {String url = "http://jsonplaceholder.typicode.com/posts/1"; PostDTO postDTO = restTemplate.getForObject (url, PostDTO.class); System.out.println (postDTO.toString ());}
The output print result is as follows:
POJO is defined as follows, according to the data format of JSON String.
@ Datapublic class PostDTO {private int userId; private int id; private String title; private String body;} 1.3. Receive the result of the request in an array
Visit http://jsonplaceholder.typicode.com/posts to get the request result in the way of JSON array
The next step is how we receive it, and it's easy to use it. Write a unit test case in the Spring Boot environment and receive the request results as an array.
@ Testpublic void testArrays () {String url = "http://jsonplaceholder.typicode.com/posts"; PostDTO [] postDTOs = restTemplate.getForObject (url, PostDTO [] .class); System.out.println (" array length: "+ postDTOs.length);}
The result of the request is correctly received as an array, and the output is as follows:
Array length: 1001.4. Several ways of using placeholder to transfer parameters
The following requests are all accessing "http://jsonplaceholder.typicode.com/posts/1", but using placeholder syntax to be more flexible in business use."
Pass parameters in the form of placeholders:
String url = "http://jsonplaceholder.typicode.com/{1}/{2}";PostDTO postDTO = restTemplate.getForObject (url, PostDTO.class," posts ", 1)
Another form of using placeholders:
String url = "http://jsonplaceholder.typicode.com/{type}/{id}";String type =" posts "; int id = 1PostDTO postDTO = restTemplate.getForObject (url, PostDTO.class, type, id)
We can also use map mount parameters:
String url = "http://jsonplaceholder.typicode.com/{type}/{id}";Map map = new HashMap (); map.put (" type "," posts "); map.put (" id ", 1); PostDTO postDTO = restTemplate.getForObject (url, PostDTO.class, map); II. GetForEntity () method
All the above getForObject request parameter passing methods can be used by getForEntity, and the method is almost the same, except that there is a slight difference when the return result is received. Use ResponseEntity responseEntity to receive the response result. Get the response body with responseEntity.getBody (). The content of the response body is consistent with the result returned by the getForObject method. The rest of the response information is that getForEntity has more content than getForObject.
HttpStatus statusCode = responseEntity.getStatusCode (); get the overall response status information int statusCodeValue = responseEntity.getStatusCodeValue (); get the response code value HttpHeaders headers = responseEntity.getHeaders (); get the response first class
@ Testpublic void testEntityPoJo () {String url = "http://jsonplaceholder.typicode.com/posts/5"; ResponseEntity responseEntity = restTemplate.getForEntity (url, PostDTO.class); PostDTO postDTO = responseEntity.getBody (); / / get the response body System.out.println (" HTTP response body: "+ postDTO.toString ()); / / the following is more content of getForEntity than getForObject HttpStatus statusCode = responseEntity.getStatusCode () / / get response code int statusCodeValue = responseEntity.getStatusCodeValue (); / / get response code value HttpHeaders headers = responseEntity.getHeaders (); / / get response header System.out.println ("HTTP response status:" + statusCode); System.out.println ("HTTP response status code:" + statusCodeValue); System.out.println ("HTTP Headers Information:" + headers);}
Output print result
Thank you for reading! This is the end of the article on "how to use RestTemplate GET request". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.