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?
In order to comprehensively display the developme...
666clouds has launched a special promotion during...
An operator insider confirmed to IT Times, "...
Recently, I briefly studied the development histo...
Technology has changed the way we conduct diagnos...
[51CTO.com original article] On December 1-2, 201...
In the first half of 2020, affected by the epidem...
Domestic 5G construction is still in full swing, ...
Project site selection is the starting point for ...
Have you paid attention to the wave of "bomb...
[51CTO.com original article] At the just conclude...
In this section, Rui Ge will continue to show you...
Power over Ethernet (PoE) is a technology that tr...
Three companies reported: 5G driving effect is ev...