The New Year is over, and Brother Song has been moving bricks for three days. After the epidemic was relaxed, this year was particularly relaxing and comfortable. A particularly depressing thing in my heart was removed. I saw in the news that there were tourists from all over the country and the Tang Dynasty City in Xi'an was crowded with tourists. It really seemed like I was back in 2019. My friends in the circle were all happy, and life was still very good. I haven't posted a technical article for a long time. I recently returned to work and have time to code technology again in the evening. Today we will talk about a new thing in Spring Boot3, declarative HTTP calls. 1. OriginSpring Boot 3 was officially released at the end of last year, and I have also had a taste of it. Recently, I will have time to talk to my friends about what new things Spring Boot 3 has brought us. Today we will first take a look at the declarative HTTP interface. Friends who have used Spring Cloud know that in the Spring Cloud family, we can use RestTemplate or OpenFeign to be responsible for inter-process communication (of course there are other ways such as message-driven microservices based on message middleware or gRPC-based calls, etc.). We can treat RestTemplate as an ordinary HTTP calling tool. Different from other HTTP clients, RestTemplate is particularly convenient for calling RESTful-style interfaces. However, OpenFeign is more convenient than RestTemplate. Remote calls can be implemented through interface declarations. Song Ge has talked about the specific usage in the previous video, so I will not repeat it here. In the past, if we wanted to use declarative HTTP calls, we had to use OpenFeign, which required third-party dependencies. Starting from Spring 6 (Spring Boot 3), Spring itself provides similar functions, which can also easily implement declarative HTTP calls through the @HttpExchange annotation. In the future, there will be another option for cross-service calls. 2. UseNext, Songge will use a case to demonstrate the specific usage of @HttpExchange annotation. First, we create a common Spring Boot project named server. In this common Spring Boot project, we only need to provide a simple test interface, as follows: @RestController This should not be difficult for everyone, so I won’t say much about it. Now suppose I have another service named client, and I want to call the interface provided by the server in the client. First, let's create the client project. Please note that when creating it, we need to add not only Web dependencies, but also Reactive Web, because the underlying layer of @HttpExchange is based on WebClient, which is provided by Reactive Web: After the creation is complete, we can then declare the Http interface: @HttpExchange ( "/server" ) These usages are very similar to @RequestMapping and @GetMapping that we commonly use in SpringMVC:
After the interface is declared, it is not over yet. We still need to configure it before we can use it. As follows: @Configuration This configuration mainly has two aspects:
After all the configurations are completed, we can then directly inject the ToDoService instance wherever we need it. Here is a simple example for your reference: @SpringBootTest Okay, here is a simple example, you may want to try it out. In the future, declarative service calls can be implemented without OpenFeign~ |
>>: Do you know the origin and function of Wi-Fi?
[[391275]] Zookeeper achieves the final consisten...
On March 2, MWC 2023 was held in Barcelona, Spa...
[[391593]] The pandemic has forced government age...
On June 1-2, 2016, with the support of China SDN ...
With the rapid development of "Internet +&qu...
Recently, the three major telecom operators have ...
Wi-Fi has had a huge impact on mobile computing, ...
[[177681]] On November 28, the Ministry of Indust...
On the evening of May 10th, Beijing time, Microso...
Recently, F5, the world's leading multi-cloud...
IMIDC, also known as Rainbow Cloud, is a local op...
80VPS has launched a mid-year promotion, offering...
In the past, people settled near the banks of riv...
This article will serve as a guide for using MTP ...
2020 is the first year of large-scale constructio...