1、RequestInterfaceInitConfiguration实现ApplicationRunner接口实现项目启动运行
package com.hdy.manage.configuration;
import com.hdy.manage.data.entity.SysInterface;
import com.hdy.manage.repository.SysInterfaceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
@Slf4j
public class RequestInterfaceInitConfiguration implements ApplicationRunner {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Autowired
private SysInterfaceRepository sysInterfaceRepository;
@Value("${server.servlet.context-path}")
private String servletPath;
@Value("${server.port}")
private String serverPort;
@Value("${request.interface.scan.package}")
private String scanPackage;
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("request interface path init start...");
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> handlerMethodEntry : handlerMethodMap.entrySet()) {
HandlerMethod handlerMethod = handlerMethodEntry.getValue();
Class<?> activeClass = handlerMethod.getBeanType();
String packageName = activeClass.getPackage().getName();
if (!packageName.equals(scanPackage)) {
continue;
}
SysInterface sysInterface = new SysInterface();
sysInterface.setServerPort(serverPort);
sysInterface.setServletPath(servletPath);
sysInterface.setClassName(activeClass.getSimpleName());
sysInterface.setClassReference(activeClass.getName());
RequestMapping requestMapping = activeClass.getAnnotation(RequestMapping.class);
if (null != requestMapping) {
String[] path = requestMapping.value();
if (path.length > 0) {
sysInterface.setClassPath(path[0]);
}
sysInterface.setClassDesc(requestMapping.name());
}
Method method = handlerMethod.getMethod();
sysInterface.setMethodName(method.getName());
// 获取方法上的注解
PostMapping postMapping = method.getAnnotation(PostMapping.class);
if (null == postMapping) {
GetMapping getMapping = method.getAnnotation(GetMapping.class);
if (null == getMapping) {
sysInterface.setRequestMethod("unknown");
} else {
sysInterface.setRequestMethod("GET");
String[] path = getMapping.value();
if (path.length > 0) {
sysInterface.setMethodPath(path[0]);
}
sysInterface.setMethodDesc(getMapping.name());
}
} else {
sysInterface.setRequestMethod("POST");
String[] path = postMapping.value();
if (path.length > 0) {
sysInterface.setMethodPath(path[0]);
}
sysInterface.setMethodDesc(postMapping.name());
}
// 获取方法的参数注解
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
List<String> parameterAnnotationNames = new ArrayList<>();
if (null != parameterAnnotations && parameterAnnotations.length > 0) {
for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] twoAnnotation = parameterAnnotations[i];
if (null != twoAnnotation && twoAnnotation.length > 0) {
for (int j = 0; j < twoAnnotation.length; j++) {
Annotation annotation = twoAnnotation[j];
parameterAnnotationNames.add(annotation.annotationType().getName());
if (annotation instanceof Validated) {
// 进行了参数校验
sysInterface.setIsValidated(true);
} else if (annotation instanceof RequestBody) {
} else if (annotation instanceof RequestParam) {
String value = ((RequestParam) annotation).value();
}
}
}
}
}
String fullPath = sysInterface.getClassPath() + sysInterface.getMethodPath();
sysInterface.setFullPath(fullPath);
List<SysInterface> sysInterfaceList = sysInterfaceRepository.findByFullPath(fullPath);
if (CollectionUtils.isEmpty(sysInterfaceList)) {
sysInterfaceRepository.save(sysInterface);
} else {
SysInterface existInterface = sysInterfaceList.get(0);
existInterface.setClassName(sysInterface.getClassName());
existInterface.setClassReference(sysInterface.getClassReference());
existInterface.setClassPath(sysInterface.getClassPath());
existInterface.setClassDesc(sysInterface.getClassDesc());
existInterface.setMethodName(sysInterface.getMethodName());
existInterface.setMethodPath(sysInterface.getMethodPath());
existInterface.setMethodDesc(sysInterface.getMethodDesc());
existInterface.setRequestMethod(sysInterface.getRequestMethod());
existInterface.setServerPort(sysInterface.getServerPort());
existInterface.setServletPath(sysInterface.getServletPath());
existInterface.setIsValidated(sysInterface.getIsValidated());
sysInterfaceRepository.save(existInterface);
}
log.info("request interface path:" + fullPath);
}
log.info("request interface path init end...");
}
}
- Controller类上需要添加注解@RestController和@RequestMapping(value = "/xxx", name = "XXX")
- 接口方法上需要添加注解@GetMapping(value = "/xxx", name = "XXX")或者@PostMapping(value = "/xxx", name = "XXX")
- scanPackage可以配置什么包下的Controller接口会被获取记录
- RequestMappingHandlerMapping也是在DispatcherServlet的初始化过程中自动加载的,默认会自动加载所有实现HandlerMapping接口的bean
2、SysInterface接口实体类
package com.hdy.manage.data.entity;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Table(name = "sys_interface")
public class SysInterface implements java.io.Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name = "class_name")
private String className;
@Column(name = "class_path")
private String classPath;
@Column(name = "class_reference")
private String classReference;
@Column(name = "class_desc")
private String classDesc;
@Column(name = "method_name")
private String methodName;
@Column(name = "method_path")
private String methodPath;
@Column(name = "method_desc")
private String methodDesc;
@Column(name = "request_method")
private String requestMethod;
@Column(name = "full_path")
private String fullPath;
@Column(name = "server_port")
private String serverPort;
@Column(name = "servlet_path")
private String servletPath;
@Column(name = "is_validated")
private Boolean isValidated = false;
@Column(name = "effect_status")
private Boolean effectStatus;
@Column(name = "create_time", insertable = false, updatable = false)
private java.util.Date createTime;
@Column(name = "update_time", insertable = false, updatable = false)
private java.util.Date updateTime;
}