In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use RestTemplate in Spring or non-Spring environments". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use RestTemplate in Spring or non-Spring environments.
What is RestTemplate?
RestTemplate is a synchronous blocking client that executes HTTP requests, encapsulating a more easy-to-use template method, API, on the basis of HTTP client libraries such as JDK HttpURLConnection,Apache HttpComponents,okHttp, etc. In other words, RestTemplate is an encapsulation, and the underlying implementation is still some HTTP clients commonly used in java application development. However, compared with directly using the underlying HTTP client library, its operation is more convenient and fast, and can greatly improve our development efficiency.
RestTemplate, as part of the spring-web project, was introduced in Spring version 3.0. The RestTemplate class provides a very convenient way to access HTTP-based Web services by providing overloaded methods for HTTP methods such as GET,POST,PUT,DELETE and so on. If your Web service API is based on the standard RESTful style design, the effect will be more perfect.
According to the official Spring documentation and source code, RestTemplate may be deprecated in future releases because they have introduced WebClient as a non-blocking Reactive HTTP client in Spring 5. But RestTemplate is still heavily dependent on many projects in the Spring community, such as Spring Cloud. In addition, RestTemplate is a client-side API encapsulation, and the demand for non-blocking Reactive programming is not that high compared with the server side.
2. Using RestTemplate in non-Spring environment
In order to facilitate the follow-up development and testing, first introduce a website to you. JSONPlaceholder is a website that provides free online REST API, and we can use the url address it provides to test network requests and request parameters. Or when our program needs to get some simulation data, simulation pictures can also use it.
RestTemplate is a rest client of spring, under the package spring-web. Although this package is called spring-web, its RestTemplate can be used outside the Spring environment.
Org.springframework spring-web 5.2.6.RELEASE
Test the Hello world, send a GET request using RestTemplate, and print out the result of the JSON data from the request.
@ Testpublic void simpleTest () {RestTemplate restTemplate = new RestTemplate (); String url = "http://jsonplaceholder.typicode.com/posts/1"; String str = restTemplate.getForObject (url, String.class); System.out.println (str);}
The server side is the JSONPlaceholder website, which provides us with the server side API. It should be noted that: "http://jsonplaceholder.typicode.com/posts/1" service URL, although there is the word posts in URL, but its English meaning is: post or announcement, not our HTTP Post protocol."
So say "http://jsonplaceholder.typicode.com/posts/1", the requested data is: Post announcement resource with an id of 1." The print result is as follows:
Here we just demonstrate the most basic use of RestTemplate, RestTemplate will be written into a series of articles, please pay attention.
3. Use RestTemplate in Spring environment
Change maven coordinates from spring-web to spring-boot-starter-web
Org.springframework.boot spring-boot-starter-web
Initialize the RestTemplate configuration to a Bean. This initialization method uses the HttpURLConnection that comes with JDK as the underlying HTTP client implementation. We can also switch the underlying implementation to Apache HttpComponents,okHttp and so on, which we will introduce in later chapters.
@ Configurationpublic class ContextConfig {/ / default uses HttpURLConnection that comes with JDK as the underlying implementation @ Bean public RestTemplate restTemplate () {RestTemplate restTemplate = new RestTemplate (); return restTemplate;}}
Where you need to use RestTemplate, just inject and use it.
@ Resource / / @ AutoWired private RestTemplate restTemplate; here, I believe you have a better understanding of "how RestTemplate is used in Spring or non-Spring environments". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.