Commit 8e27999a by flying-cattle

加swagger配置文件自动生成

parent 03bd84cb
<?xml version="1.0" encoding="UTF-8"?> <?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"> 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> <modelVersion>4.0.0</modelVersion>
...@@ -59,11 +60,7 @@ ...@@ -59,11 +60,7 @@
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version> <version>${mysql-connector-java.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.freemarker</groupId> <groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId> <artifactId>freemarker</artifactId>
......
...@@ -69,6 +69,8 @@ public class BasisInfo implements Serializable{ ...@@ -69,6 +69,8 @@ public class BasisInfo implements Serializable{
private String controllerUrl; private String controllerUrl;
private String swaggerConfigUrl;
private String idType; private String idType;
private String idJdbcType; private String idJdbcType;
...@@ -94,5 +96,6 @@ public class BasisInfo implements Serializable{ ...@@ -94,5 +96,6 @@ public class BasisInfo implements Serializable{
this.serviceUrl = serviceUrl; this.serviceUrl = serviceUrl;
this.serviceImplUrl = serviceImplUrl; this.serviceImplUrl = serviceImplUrl;
this.controllerUrl = controllerUrl; this.controllerUrl = controllerUrl;
this.swaggerConfigUrl=controllerUrl.substring(0, controllerUrl.lastIndexOf("."))+".config";
} }
} }
...@@ -8,7 +8,6 @@ package com.github.mybatis.fl.test; ...@@ -8,7 +8,6 @@ package com.github.mybatis.fl.test;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import com.github.mybatis.fl.entity.BasisInfo; import com.github.mybatis.fl.entity.BasisInfo;
import com.github.mybatis.fl.util.EntityInfoUtil; import com.github.mybatis.fl.util.EntityInfoUtil;
import com.github.mybatis.fl.util.Generator; import com.github.mybatis.fl.util.Generator;
...@@ -47,12 +46,15 @@ public class TestMain { ...@@ -47,12 +46,15 @@ public class TestMain {
bi.setEntityComment(CLASSCOMMENT); bi.setEntityComment(CLASSCOMMENT);
try { try {
bi=EntityInfoUtil.getInfo(bi); bi=EntityInfoUtil.getInfo(bi);
String aa1=Generator.createEntity("E:\\b_test_work\\", bi).toString(); String fileUrl="E:\\b_test_work\\";//生成文件存放位置
String aa2=Generator.createDao("E:\\b_test_work\\", bi).toString(); String aa1=Generator.createEntity(fileUrl, bi).toString();
String aa3=Generator.createDaoImpl("E:\\b_test_work\\", bi).toString(); String aa2=Generator.createDao(fileUrl, bi).toString();
String aa4=Generator.createService("E:\\b_test_work\\", bi).toString(); String aa3=Generator.createDaoImpl(fileUrl, bi).toString();
String aa5=Generator.createServiceImpl("E:\\b_test_work\\", bi).toString(); String aa4=Generator.createService(fileUrl, bi).toString();
String aa6=Generator.createController("E:\\b_test_work\\", 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(aa1);
System.out.println(aa2); System.out.println(aa2);
...@@ -60,6 +62,7 @@ public class TestMain { ...@@ -60,6 +62,7 @@ public class TestMain {
System.out.println(aa4); System.out.println(aa4);
System.out.println(aa5); System.out.println(aa5);
System.out.println(aa6); System.out.println(aa6);
System.out.println(aa7);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -32,7 +32,7 @@ public class Generator { ...@@ -32,7 +32,7 @@ public class Generator {
public static final String SERVICE="service"; public static final String SERVICE="service";
public static final String SERVICE_IMPL="serviceImpl"; public static final String SERVICE_IMPL="serviceImpl";
public static final String CONTROLLER="controller"; public static final String CONTROLLER="controller";
public static final String SWAGGER_CONFIG="swaggerConfig";
//①创建实体类 //①创建实体类
public static JsonResult createEntity(String url,BasisInfo bi) { public static JsonResult createEntity(String url,BasisInfo bi) {
...@@ -76,8 +76,12 @@ public class Generator { ...@@ -76,8 +76,12 @@ public class Generator {
String fileUrl= getGeneratorFileUrl(url, bi.getControllerUrl(), bi.getEntityName(), CONTROLLER); String fileUrl= getGeneratorFileUrl(url, bi.getControllerUrl(), bi.getEntityName(), CONTROLLER);
return FreemarkerUtil.createFile(bi, "controller.ftl", fileUrl); 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){ public static String getGeneratorFileUrl(String url,String packageUrl,String entityName, String type){
if (type.equals("entity")) { if (type.equals("entity")) {
return url+pageToUrl(packageUrl)+entityName+".java"; return url+pageToUrl(packageUrl)+entityName+".java";
...@@ -91,6 +95,8 @@ public class Generator { ...@@ -91,6 +95,8 @@ public class Generator {
return url+pageToUrl(packageUrl)+entityName+"ServiceImpl.java"; return url+pageToUrl(packageUrl)+entityName+"ServiceImpl.java";
}else if(type.equals("controller")) { }else if(type.equals("controller")) {
return url+pageToUrl(packageUrl)+entityName+"Controller.java"; return url+pageToUrl(packageUrl)+entityName+"Controller.java";
}else if(type.equals("swaggerConfig")){
return url+pageToUrl(packageUrl)+entityName+"Config.java";
} }
return null; 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 @@ ...@@ -6,12 +6,9 @@
*/ */
package ${controllerUrl}; package ${controllerUrl};
import com.item.util.JsonResult; import com.item.util.JsonResult;
import ${entityUrl}.${entityName}; import ${entityUrl}.${entityName};
import ${serviceUrl}.${entityName}Service; import ${serviceUrl}.${entityName}Service;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -20,7 +17,6 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -20,7 +17,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
......
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