0%

反射获取swagger描述,并插入到redis

反射获取swagger描述,并插入到redis

反射获取swagger描述,并插入到redis

  1. redis序列化配置
  2. 实现SpringBoot的CommandLineRunner接口,作用:项目启动时执行run方法
  3. 反射获取所有的Controller类
  4. 获取Controller类上的swagger注解,取需要的值
  5. 遍历所有的Controller类,获取每个类的所有方法
  6. 获取每个方法上的swagger注解,取需要的值
  7. 插入到redis中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.xiaoruiit.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* Redis序列化配置
*
*/
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;

@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.xiaoruiit.common.config;

import cn.hutool.core.util.StrUtil;
import com.hanxxiaorui.common.constant.SwaggerConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
* @author hxr
*/
@Slf4j
@Component
public class BaseSwaggerCache implements CommandLineRunner {// 实现SpringBoot的CommandLineRunner接口,项目启动时执行run方法

@Value("${spring.application.name}")
private String appName;

@Autowired
WebApplicationContext applicationContext;

@Autowired
RedisTemplate redisTemplate;

@Override
public void run(String... args) {
try {
getAllUrlAndSetRedisForLogManger();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取swagger注解并放入redis
*/
public void getAllUrlAndSetRedisForLogManger() {
Map<String, Object> result = new HashMap<>();
// 获取带有Controller注解的类
Map<String, Object> beansMap = applicationContext.getBeansWithAnnotation(Controller.class);
for (String key : beansMap.keySet()) {
// 反射获取每个controller类
Class<?> clazz = beansMap.get(key).getClass();
// 获取因@Validated注解,导致Cglib代理过的父类。 @Validated会在原类上生成子类,子类不继承父类注解。
Class<?> userClass = ClassUtils.getUserClass(clazz);

String classPath = this.getRequestMappingPath(userClass);
String apiValue = this.getApiValue(userClass);
// 获取类的所有方法
Method[] methods = userClass.getMethods();
for (Method method : methods) {
String methodPath = this.getMethodRequestMappingPath(method);
String apiOperationPath = this.getApiOperationValue(method);

if (StrUtil.isNotEmpty(methodPath)
&& StrUtil.isNotEmpty(apiValue)
&& StrUtil.isNotEmpty(apiOperationPath)) {
apiValue = apiValue.replace("接口", "");
result.put(this.deleteBrackets(appName + "/" + classPath + methodPath), apiValue + "|" + apiOperationPath);
}
}
}
// 插入到redis中
redisTemplate.opsForHash().putAll(SwaggerConstant.SWAGGER_HASH_KEY, result);
}


/**
* 获取类上注解@RequestMapping中的value的值 如:"/back/management"
*/
private String getRequestMappingPath(Class<?> clazz) {
RequestMapping annotation = clazz.getAnnotation(RequestMapping.class);
if (annotation == null) {
return "";
}
String[] values = annotation.value();
String path = "";
for (String value : values) {
if (StrUtil.isNotEmpty(value)) {
path = value;
break;
}
}
return path;
}

/**
* 获取类上注解@Api中tags的值 如:"后台管理"
*/
private String getApiValue(Class<?> clazz) {
Api api = clazz.getAnnotation(Api.class);
if (api == null) {
String clazzSimpleName = clazz.getSimpleName();
return clazzSimpleName;
}
String[] tags = api.tags();
String apiValue = "";
for (String tag : tags) {
if (StrUtil.isNotEmpty(tag)) {
apiValue = tag;
break;
}
}
if (StrUtil.isEmpty(apiValue)) {
String value = api.value();
if (StrUtil.isNotEmpty(value)) {
apiValue = value;
}
}
return apiValue;
}

/**
* 获取方法上注解@RequestMapping、@GetMapping等里边value的值 如:"/add"
*/
private String getMethodRequestMappingPath(Method method) {
GetMapping getMapping = method.getAnnotation(GetMapping.class);
PostMapping postMapping = method.getAnnotation(PostMapping.class);
DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);

String[] values = getMapping != null ? getMapping.value()
: postMapping != null ? postMapping.value()
: requestMapping != null ? requestMapping.value() : deleteMapping != null ? deleteMapping.value()
: new String[]{};

String path = "";
for (String value : values) {
if (StrUtil.isNotEmpty(value)) {
path = value;
break;
}
}
return path;
}

/**
* 获取方法上注解@ApiOperation里边value的值 如:"新增"
*/
private String getApiOperationValue(Method method) {
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
if (apiOperation == null) {
String methodName = method.getName();
return methodName;
}
String value = apiOperation.value();
if (StrUtil.isEmpty(value)) {
return "";
}
return value;
}

/**
* 将RESTful风格/{id},记录为/*
*/
private String deleteBrackets(String src) {
if (StrUtil.isEmpty(src)) {
return "";
}
src = src.replace("//", "/");
int i = src.indexOf("/{");
if (i != -1) {
return src.substring(0, i) + "/*";
}
return src;
}

}