总结
本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!
MySQL50道高频面试题整理:
配置@RetrofitScan注解
=================
你可以给带有 @Configuration 的类配置@RetrofitScan,或者直接配置到spring-boot的启动类上,如下:
@SpringBootApplication
@RetrofitScan(“com.github.lianjiatech.retrofit.spring.boot.test”)
public class RetrofitTestApplication { public static void main(String[] args) {
SpringApplication.run(RetrofitTestApplication.class, args);
}}
定义http接口
========
接口必须使用@RetrofitClient注解标记!http相关注解可参考官方文档:retrofit官方文档。
@RetrofitClient(baseUrl = “${test.baseUrl}”)
public interface HttpApi { @GET(“person”)
Result getPerson(@Query(“id”) Long id);
}
注入使用
====
将接口注入到其它Service中即可使用。
@Service
public class TestService {
@Autowired
private HttpApi httpApi;
public void test() {
// 通过httpApi发起http请求
}
}
只要通过上述几个步骤,就能实现通过接口发送http请求了,真的很简单。如果你在spring-boot项目里面使用过mybatis,相信你对这种使用方式会更加熟悉。接下来我们继续介绍一下retrofit-spring-boot-starter更高级一点的功能。
注解式拦截器
======
很多时候,我们希望某个接口下的某些http请求执行统一的拦截处理逻辑。这个时候可以使用注解式拦截器。使用的步骤主要分为2步:
-
继承BasePathMatchInterceptor编写拦截处理器;
-
接口上使用@Intercept进行标注。
下面以给指定请求的url后面拼接timestamp时间戳为例,介绍下如何使用注解式拦截器。
继承BasePathMatchInterceptor编写拦截处理器
=================================
@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);
}}
接口上使用@Intercept进行标注
===================
@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);
}
上面的@Intercept配置表示:拦截HttpApi接口下/api/**路径下(排除/api/test/savePerson)的请求,拦截处理器使用TimeStampInterceptor。
扩展注解式拦截器
========
有的时候,我们需要在拦截注解动态传入一些参数,然后再执行拦截的时候需要使用这个参数。这种时候,我们可以扩展实现自定义拦截注解。自定义拦截注解必须使用@InterceptMark标记,并且注解中必须包括include()、exclude()、handler()属性信息。使用的步骤主要分为3步:
-
自定义拦截注解
-
继承BasePathMatchInterceptor编写拦截处理器
-
接口上使用自定义拦截注解;
例如我们需要在请求头里面动态加入accessKeyId、accessKeySecret签名信息才能正常发起http请求,这个时候可以**自定义一个加签拦截器注解@Sign**来实现。下面以自定义@Sign拦截注解为例进行说明。
自定义@Sign注解
==========
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@InterceptMark
public @interface Sign {
/**
-
密钥key
-
支持占位符形式配置。
-
@return
*/
String accessKeyId();
/**
-
密钥
-
支持占位符形式配置。
-
@return
*/
String accessKeySecret();
/**
-
拦截器匹配路径
-
@return
*/
String[] include() default {“/**”};
/**
-
拦截器排除匹配,排除指定路径拦截
-
@return
*/
String[] exclude() default {};
/**
-
处理该注解的拦截器类
-
优先从spring容器获取对应的Bean,如果获取不到,则使用反射创建一个!
-
@return
*/
Class<? extends BasePathMatchInterceptor> handler() default SignInterceptor.class;
}
扩展自定义拦截注解有以下2点需要注意:
-
自定义拦截注解必须使用@InterceptMark标记。
-
注解中必须包括include()、exclude()、handler()属性信息。
实现SignInterceptor
=================
@Component
public class SignInterceptor extends BasePathMatchInterceptor {
private String accessKeyId;
private String accessKeySecret;
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
} public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
} @Override
public Response doIntercept(Chain chain) throws IOException {
Request request = chain.request(); Request newReq = request.newBuilder() .addHeader(“accessKeyId”, accessKeyId)
.addHeader(“accessKeySecret”, accessKeySecret)
.build(); return chain.proceed(newReq);
}}
上述accessKeyId和accessKeySecret字段值会依据@Sign注解的accessKeyId()和accessKeySecret()值自动注入,如果@Sign指定的是占位符形式的字符串,则会取配置属性值进行注入。另外,accessKeyId和accessKeySecret字段必须提供setter方法。
接口上使用@Sign
==========
@RetrofitClient(baseUrl = “${test.baseUrl}”)
@Sign(accessKeyId = “ t e s t . a c c e s s K e y I d " , a c c e s s K e y S e c r e t = " {test.accessKeyId}", accessKeySecret = " test.accessKeyId",accessKeySecret="{test.accessKeySecret}”, exclude = {“/api/test/person”})
public interface HttpApi { @GET(“person”)
Result getPerson(@Query(“id”) Long id);
@POST(“savePerson”)
Result savePerson(@Body Person person);
}
这样就能在指定url的请求上,自动加上签名信息了。
连接池管理
=====
默认情况下,所有通过Retrofit发送的http请求都会使用max-idle-connections=5 keep-alive-second=300的默认连接池。当然,我们也可以在配置文件中配置多个自定义的连接池,然后通过@RetrofitClient的poolName属性来指定使用。比如我们要让某个接口下的请求全部使用poolName=test1的连接池,代码实现如下:
-
配置连接池。 retrofit: # 连接池配置 pool: test1: max-idle-connections: 3 keep-alive-second: 100 test2: max-idle-connections: 5 keep-alive-second: 50 复制代码
-
通过@RetrofitClient的poolName属性来指定使用的连接池。 @RetrofitClient(baseUrl = “${test.baseUrl}”, poolName=“test1”) public interface HttpApi { @GET(“person”) Result getPerson(@Query(“id”) Long id); } 复制代码
日志打印
====
很多情况下,我们希望将http请求日志记录下来。通过@RetrofitClient的logLevel和logStrategy属性,您可以指定每个接口的日志打印级别以及日志打印策略。retrofit-spring-boot-starter支持了5种日志打印级别(ERROR, WARN, INFO, DEBUG, TRACE),默认INFO;支持了4种日志打印策略(NONE, BASIC, HEADERS, BODY),默认BASIC。4种日志打印策略含义如下:
-
NONE:No logs.
-
BASIC:Logs request and response lines.
-
HEADERS:Logs request and response lines and their respective headers.
-
BODY:Logs request and response lines and their respective headers and bodies (if present).
retrofit-spring-boot-starter默认使用了DefaultLoggingInterceptor执行真正的日志打印功能,其底层就是okhttp原生的HttpLoggingInterceptor。当然,你也可以自定义实现自己的日志打印拦截器,只需要继承BaseLoggingInterceptor(具体可以参考DefaultLoggingInterceptor的实现),然后在配置文件中进行相关配置即可。
最后
金三银四马上就到了,希望大家能好好学习一下这些技术点
学习视频:
大厂面试真题:
r(具体可以参考DefaultLoggingInterceptor的实现),然后在配置文件中进行相关配置即可。
最后
金三银四马上就到了,希望大家能好好学习一下这些技术点
学习视频:
[外链图片转存中…(img-6mEycwow-1715091192535)]
大厂面试真题:
[外链图片转存中…(img-MhAjutxW-1715091192536)]