In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Why give up using RestTemplate in Spring Boot". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Why give up using RestTemplate in Spring Boot".
If you are still using RestTemplate or okhttp directly in your project, or based on their encapsulated HttpUtils, you can try using Retrofit.
Retrofit-spring-boot-starter realizes the rapid integration of Retrofit and spring-boot framework, and supports some functional enhancements, which greatly simplifies the development of http interface calls under spring-boot projects. Let's take a look at how easy it is for a spring-boot project to send a http request directly through retrofit-spring-boot-starter.
Retrofit officially does not provide starter for rapid integration with spring-boot. Retrofit-spring-boot-starter is encapsulated by the author, has been used in the production environment, and is very stable. Give me a star if you like.
Https://github.com/LianjiaTech/retrofit-spring-boot-starter
Introduce dependency
Com.github.lianjiatech retrofit-spring-boot-starter 2.0.2
Configure @ RetrofitScan annotation
You can configure @ RetrofitScan for a class with @ Configuration, or configure it directly to the startup class of spring-boot, as follows:
SpringBootApplication @ RetrofitScan ("com.github.lianjiatech.retrofit.spring.boot.test") public class RetrofitTestApplication {public static void main (String [] args) {SpringApplication.run (RetrofitTestApplication.class, args);}}
Define http Interfac
Interfaces must be marked with @ RetrofitClient annotations!
For comments related to http, please refer to the official documentation:
Https://square.github.io/retrofit/
RetrofitClient (baseUrl = "${test.baseUrl}") public interface HttpApi {@ GET ("person") Result getPerson (@ Query ("id") Long id);}
Injection use
You can use the interface by injecting it into other Service.
@ Service public class TestService {@ Autowired private HttpApi httpApi; public void test () {/ / initiate a http request via httpApi}}
As long as you go through the above steps, you can send http requests through the interface, which is really simple. If you have used mybatis in a spring-boot project, I believe you will be more familiar with this method of use.
Let's move on to the more advanced features of retrofit-spring-boot-starter.
Annotated interceptor
In many cases, we want some http requests under an interface to perform uniform interception processing logic. An annotated interceptor can be used at this time. The steps to use are mainly divided into two steps:
Inherit BasePathMatchInterceptor to write intercept processor
The interface is annotated with @ Intercept.
Here is an example of how to use an annotated interceptor by stitching the timestamp timestamp after the url of the specified request.
Inherit BasePathMatchInterceptor to write intercept processor
@ Component public class TimeStampInterceptor extends BasePathMatchInterceptor {@ Override public Response doIntercept (Chain chain) throws IOException {Request request = chain.request (); HttpUrl url = request.url (); long timestamp = System.currentTimeMillis (); HttpUrl newUrl = url.newBuilder () .addQueryParameter ("timestamp", String.valueOf (timestamp)) .build () Request newRequest = request.newBuilder () .url (newUrl) .build (); return chain.proceed (newRequest);}}
Use @ Intercept for annotation on the interface
RetrofitClient (baseUrl = "${test.baseUrl}") @ Intercept (handler = TimeStampInterceptor.class, include = {"/ api/**"}, exclude = "/ api/test/savePerson") public interface HttpApi {@ GET ("person") Result getPerson (@ Query ("id") Long id); @ POST ("savePerson") Result savePerson (@ Body Person person);}
The above @ Intercept configuration indicates that requests under the / api/** path under the HttpApi interface (excluding / api/test/savePerson) are intercepted, and the processor is intercepted using TimeStampInterceptor. Recommended: a summary of the 100 interview questions
Extended annotated interceptor
Sometimes we need to use this parameter when we pass in some parameters dynamically in the intercept annotation and then perform the intercept. At this point, we can extend the implementation of custom intercept annotations.
Custom intercept annotations must use the @ InterceptMark tag, and the annotations must include include (), exclude (), and handler () attribute information. The steps to use are mainly divided into three steps:
Custom intercept comment
Inherit BasePathMatchInterceptor to write intercept processor
Use custom intercept comments on the interface
For example, we need to dynamically add accessKeyId and accessKeySecret signature information to the request header to launch the http request normally. In this case, you can customize a signature interceptor annotation @ Sign to implement it. Take the custom @ Sign interception annotation as an example.
Custom @ Sign annotation
@ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.TYPE) @ Documented @ InterceptMark public @ interface Sign {/ * key key * supports placeholder configuration. * * @ return * / String accessKeyId (); / * * key * supports placeholder configuration. * * @ return * / String accessKeySecret (); / * * interceptor matching path * * @ return * / String [] include () default {"/ *"} / * interceptor excludes matching, excludes specified path interception * * @ return * / String [] exclude () default {}; / * interceptor class that handles the annotation * first gets the corresponding Bean from the spring container. If not, create one using reflection! * * @ return * / Class
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.