Commit 8e27999a by flying-cattle

加swagger配置文件自动生成

parent 03bd84cb
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
......@@ -59,11 +60,7 @@
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
......@@ -78,79 +75,79 @@
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>${maven-gpg-plugin.version}</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>${maven-gpg-plugin.version}</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
......@@ -69,6 +69,8 @@ public class BasisInfo implements Serializable{
private String controllerUrl;
private String swaggerConfigUrl;
private String idType;
private String idJdbcType;
......@@ -94,5 +96,6 @@ public class BasisInfo implements Serializable{
this.serviceUrl = serviceUrl;
this.serviceImplUrl = serviceImplUrl;
this.controllerUrl = controllerUrl;
this.swaggerConfigUrl=controllerUrl.substring(0, controllerUrl.lastIndexOf("."))+".config";
}
}
......@@ -8,7 +8,6 @@ package com.github.mybatis.fl.test;
import java.sql.SQLException;
import java.util.Date;
import com.github.mybatis.fl.entity.BasisInfo;
import com.github.mybatis.fl.util.EntityInfoUtil;
import com.github.mybatis.fl.util.Generator;
......@@ -47,12 +46,15 @@ public class TestMain {
bi.setEntityComment(CLASSCOMMENT);
try {
bi=EntityInfoUtil.getInfo(bi);
String aa1=Generator.createEntity("E:\\b_test_work\\", bi).toString();
String aa2=Generator.createDao("E:\\b_test_work\\", bi).toString();
String aa3=Generator.createDaoImpl("E:\\b_test_work\\", bi).toString();
String aa4=Generator.createService("E:\\b_test_work\\", bi).toString();
String aa5=Generator.createServiceImpl("E:\\b_test_work\\", bi).toString();
String aa6=Generator.createController("E:\\b_test_work\\", bi).toString();
String fileUrl="E:\\b_test_work\\";//生成文件存放位置
String aa1=Generator.createEntity(fileUrl, bi).toString();
String aa2=Generator.createDao(fileUrl, bi).toString();
String aa3=Generator.createDaoImpl(fileUrl, bi).toString();
String aa4=Generator.createService(fileUrl, bi).toString();
String aa5=Generator.createServiceImpl(fileUrl, bi).toString();
String aa6=Generator.createController(fileUrl, bi).toString();
String aa7=Generator.createSwaggerConfig(fileUrl, bi).toString();
//创建swagger配置
System.out.println(aa1);
System.out.println(aa2);
......@@ -60,6 +62,7 @@ public class TestMain {
System.out.println(aa4);
System.out.println(aa5);
System.out.println(aa6);
System.out.println(aa7);
} catch (SQLException e) {
e.printStackTrace();
}
......
......@@ -32,7 +32,7 @@ public class Generator {
public static final String SERVICE="service";
public static final String SERVICE_IMPL="serviceImpl";
public static final String CONTROLLER="controller";
public static final String SWAGGER_CONFIG="swaggerConfig";
//①创建实体类
public static JsonResult createEntity(String url,BasisInfo bi) {
......@@ -76,8 +76,12 @@ public class Generator {
String fileUrl= getGeneratorFileUrl(url, bi.getControllerUrl(), bi.getEntityName(), CONTROLLER);
return FreemarkerUtil.createFile(bi, "controller.ftl", fileUrl);
}
//生成文件
//⑥创建CONTROLLER
public static JsonResult createSwaggerConfig(String url,BasisInfo bi) {
String fileUrl= getGeneratorFileUrl(url,bi.getSwaggerConfigUrl(), "Swagger", SWAGGER_CONFIG);
return FreemarkerUtil.createFile(bi, "SwaggerConfig.ftl", fileUrl);
}
//生成文件路径和名字
public static String getGeneratorFileUrl(String url,String packageUrl,String entityName, String type){
if (type.equals("entity")) {
return url+pageToUrl(packageUrl)+entityName+".java";
......@@ -91,6 +95,8 @@ public class Generator {
return url+pageToUrl(packageUrl)+entityName+"ServiceImpl.java";
}else if(type.equals("controller")) {
return url+pageToUrl(packageUrl)+entityName+"Controller.java";
}else if(type.equals("swaggerConfig")){
return url+pageToUrl(packageUrl)+entityName+"Config.java";
}
return null;
}
......
/**
* @filename:${entityName}Controller ${createTime}
* @project ${project} ${version}
* Copyright(c) 2020 ${author} Co. Ltd.
* All right reserved.
*/
package ${swaggerConfigUrl};
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger核心配置文件
* ========================
* @author ${author}
* @Date ${createTime}
* ========================
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static String CONTROLLER_URL="${controllerUrl}"; //Swagger扫描的接口路径
public static String SWAGGER_TITLE="API文档-${author}"; //Swagger接口文档标题
public static String SWAGGER_DESCRIPTION="API文档"; //Swagger接口文档描述
public static String SWAGGER_VERSION="1.0"; //Swagger接口文档版本
public static String SWAGGER_URL="http://127.0.0.1:8080"; //Swagger项目服务的URL
//验证的页面http://127.0.0.1:8080/swagger-ui.html
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(CONTROLLER_URL))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(SWAGGER_TITLE)
.description(SWAGGER_DESCRIPTION)
.termsOfServiceUrl(SWAGGER_URL)
.version(SWAGGER_VERSION)
.build();
}
}
......@@ -6,12 +6,9 @@
*/
package ${controllerUrl};
import com.item.util.JsonResult;
import ${entityUrl}.${entityName};
import ${serviceUrl}.${entityName}Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -20,7 +17,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
......@@ -39,7 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* ${createTime} ${author} ${version} initialize
* ${createTime} ${author} ${version} initialize
*/
@Api(description = "${entityComment}",value="${entityComment}" )
@RestController
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment