编写自己的starter项目(battcn-starter-swagger)
自定义 starter 项目,方便其他地方调用,类似 spring.datasource.url 这种,本次以自己封装的 battcn-starter-swagger 为案例
创建一个Maven工程 battcn-starter-swagger
- pom.xml
<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>
<groupId>com.battcn</groupId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<artifactId>battcn-starter-swagger</artifactId>
<name>battcn-starter-swagger</name>
<url>http://blog.battcn.com</url>
<description>基于SpringBoot1.5.4包装的Swagger只要依赖该JAR包即可做到自动装配</description>
<version>1.0.1</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<springfox.version>2.6.1</springfox.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <!--配置maven阿里云仓库开始,不用去改maven的setting -->
<repositories>
<repository>
<id>public</id>
<name>local private nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>local private nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<!--配置maven阿里云结束 -->
</project>
- SwaggerAutoConfiguration
定义我们的 SwaggerAutoConfiguration 自动装配类
package com.battcn.framework.swagger; import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import com.battcn.framework.swagger.properties.ApiInfoProperties;
import com.battcn.framework.swagger.properties.DocketProperties;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; /**
* 自动装配类
* @author Levin
* @Q群:391619659
*/
@Configuration
@EnableConfigurationProperties(value = { SwaggerProperties.class })
@ConditionalOnProperty(name = "swagger.enable")
@EnableSwagger2
public class SwaggerAutoConfiguration { @Autowired
SwaggerProperties properties; @Bean
public Docket petApi() {
DocketProperties docket = properties.getDocket();
List<ResponseMessage> messages = new ArrayList<>();
ResponseMessage message1 = new ResponseMessageBuilder().code(200).message("操作成功")
.responseModel(new ModelRef("操作成功")).build();
ResponseMessage message2 = new ResponseMessageBuilder().code(400).message("非法请求")
.responseModel(new ModelRef("非法请求")).build();
ResponseMessage message3 = new ResponseMessageBuilder().code(501).message("如请求路径拼写不正确")
.responseModel(new ModelRef("如请求路径拼写不正确")).build();
ResponseMessage message4 = new ResponseMessageBuilder().code(502).message("服务器过载引起的错误")
.responseModel(new ModelRef("服务器过载引起的错误")).build();
messages.add(message1);
messages.add(message2);
messages.add(message3);
messages.add(message4);
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName(docket.getGroupName()).select()
.apis(RequestHandlerSelectors.basePackage(docket.getBasePackage())).paths(PathSelectors.any()).build()
.pathMapping("/").directModelSubstitute(LocalDate.class, String.class)
.genericModelSubstitutes(ResponseEntity.class).useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.POST, messages);
} private ApiInfo apiInfo() {
ApiInfoProperties apiInfo = properties.getApiInfo();
com.battcn.framework.swagger.properties.Contact contact = apiInfo.getContact();
return new ApiInfoBuilder().title(apiInfo.getTitle()).description(apiInfo.getDescription())
.termsOfServiceUrl(apiInfo.getTermsOfServiceUrl())
.contact(new Contact(contact.getName(), contact.getUrl(), contact.getEmail()))
.version(apiInfo.getVersion()).license(apiInfo.getLicense()).licenseUrl(apiInfo.getLicenseUrl())
.build();
}
}
- SwaggerProperties
这一步就是定义我们的 Properties 对象,目的就是可以被IDEA正确识别,然后可以依赖注入
package com.battcn.framework.swagger; import org.springframework.boot.context.properties.ConfigurationProperties; import com.battcn.framework.swagger.properties.ApiInfoProperties;
import com.battcn.framework.swagger.properties.DocketProperties; @ConfigurationProperties(prefix = "swagger")
public class SwaggerProperties implements java.io.Serializable { private static final long serialVersionUID = 8471755917762607584L;
private Boolean enable;
private ApiInfoProperties apiInfo;
private DocketProperties docket; public ApiInfoProperties getApiInfo() {
return apiInfo;
} public void setApiInfo(ApiInfoProperties apiInfo) {
this.apiInfo = apiInfo;
} public DocketProperties getDocket() {
return docket;
} public void setDocket(DocketProperties docket) {
this.docket = docket;
} public Boolean getEnable() {
return enable;
} public void setEnable(Boolean enable) {
this.enable = enable;
}
}
- ApiInfoProperties
package com.battcn.framework.swagger.properties;
public class ApiInfoProperties implements java.io.Serializable {
private static final long serialVersionUID = 8471755917762607584L;
private String title;
private String description;
private String termsOfServiceUrl;
private Contact contact;
private String license;
private String licenseUrl;
private String version;
// get set ...
}
- Contact
package com.battcn.framework.swagger.properties;
public class Contact {
private String name;
private String url;
private String email;
public Contact(){}
public Contact(String name, String url, String email) {
this.name = name;
this.url = url;
this.email = email;
}
// get set ...
}
- DocketProperties
package com.battcn.framework.swagger.properties;
public class DocketProperties implements java.io.Serializable {
private static final long serialVersionUID = 3342663558680329645L;
private String groupName;
private String basePackage;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getBasePackage() {
return basePackage;
}
public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}
}
- ApiParamType
package com.battcn.framework.swagger.properties; /**
* 方便Swagger 中 @ApiImplicitParam(paramType = ApiParamType.HEADER)
* @author Levin
* @Q群:391619659
*/
public final class ApiParamType { public final static String QUERY = "query";
public final static String HEADER = "header";
public final static String PATH = "path";
public final static String BODY = "body";
public final static String FORM = "form"; }
- 注意
我们需要Spring容器初始化加载我们的 SwaggerAutoConfiguration 那么必须指定初始化类路径,在 src/main/resources
创建一个 META-INF 目录,然后定义一个 spring.factories
如下: = 号的左侧不需要改,右侧为我们 SwaggerAutoConfiguration 的路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.battcn.framework.swagger.SwaggerAutoConfiguration
至此我们自己的starter包就完成了,需要使用到的项目 pom.xml 中 加入我们的starter包
建议是用JDK1.8 SpringBoot1.5.4
<dependency>
<groupId>com.battcn</groupId>
<artifactId>battcn-starter-swagger</artifactId>
<version>1.0.1</version>
</dependency>
- application.yml
#以下就是需要写的配置,注意base-package就可以了
swagger:
enable: true #是否开启swagger/默认false
api-info:
description: battcn-plus
license: battcn-plus
license-url: http://www.battcn.com
terms-of-service-url: http://www.battcn.com
title: 鏖战八方
version: 2.5.1
contact:
email: 1837307557@qq.com
name: Levin
url: http://www.battcn.com
docket:
base-package: com.battcn.platform.controller #扫描的路径/基本就是你的controller包下面
group-name: battcn-manage
- 项目地址
PS:比如A项目使用了 battcn-starter-swagger 那么只需要输入 http://${host}:${port}/swagger-ui.html 即可
编写自己的starter项目(battcn-starter-swagger)的更多相关文章
- spring项目中starter包的原理,以及自定义starter包的使用
MAVEN项目中starter的原理 一.原始方式 我们最早配置spring应用的时候,必须要经历的步骤:1.pom文件中引入相关的jar包,包括spring,redis,jdbc等等 2.通过pro ...
- 【从零开始搭建自己的.NET Core Api框架】(一)创建项目并集成swagger:1.1 创建
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- [Laravel-Swagger]如何在 Laravel 项目中使用 Swagger
如何在 Laravel 项目中使用 Swagger http://swagger.io/getting-started/ 安装依赖 swagger-php composer require zirco ...
- 用Swashbuckle给ASP.NET Core的项目自动生成Swagger的API帮助文档
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用Swashbuckle给ASP.NET Core的项目自动生成Swagger的API帮助文档.
- 编写自己的composer项目
编写自己的composer项目 composer的出现给php开发带来极大的便利, 配合phpunit的测试工具, 也可以更好的规范php开发. 尽管这些标准不是官方提供的, 但现在大部分的php ...
- 如何在spring-boot web项目中启用swagger
swagger的三个项目及其作用 我们打开swagger的官网,会发现有三个swagger相关的项目,它们分别是 swagger-editor 作用是通过写代码,生成文档描述(一个json文件或其他格 ...
- Abp项目构建、swagger及代码生成器
前段时间在学习abp,在配置swagger时踩了不少坑,特此整理一下,方便同行参考.幸运的是又发现了神奇的代码生成器,分享下亲身经验. 觉得此博客非常有用的朋友可以在右侧赞助打赏下,非常感谢大家支持. ...
- Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)
Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...
- 基于.NetCore3.1搭建项目系列 —— 使用Swagger做Api文档 (上篇)
前言 为什么在开发中,接口文档越来越成为前后端开发人员沟通的枢纽呢? 随着业务的发张,项目越来越多,而对于支撑整个项目架构体系而言,我们对系统业务的水平拆分,垂直分层,让业务系统更加清晰,从而产生一系 ...
随机推荐
- poj--1101--The Game(bfs)
The Game Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9746 Accepted: 2967 Descript ...
- codeforces 931E Logical Expression dp
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- ArrayList 扩容原理
面试中经常问到的问题之一就是List的扩容机制了,他是怎么做到扩容的,大家都能答出来底层是数组,复制一个数组来扩容,但是再具体一点来说,大家就不知道该怎么说了,如果不看源码说这么多确实就差不多了,但是 ...
- RecastNavigation(3D场景建模、网格导航)
一.RecastNavigation详解 RecastNavigation定义: RecastNavigation是一个导航寻路工具集,使用邻接的凸多边形集合描述一个3D场景,A*寻路算法使3D场景的 ...
- Run as ant build每次都执行两次-问题解决
在Eclipse里面,运行ant,整个测试流程总是执行两遍,其几天试了下在DOS命令行直接调用ant, 结果发现只执行了一次,并且内存消耗好像也没那么大了,估计是eclipse自己的问题.问题解决了, ...
- facade 模式和gateway模式的区别
原文:http://stackoverflow.com/questions/4422211/what-is-the-difference-between-facade-and-gateway-desi ...
- C# Area 双重路由如何写
在WebApi项目里面 一般除了接口, 还有管理端...一些乱七八糟的,你想展示的东西, 一种做法是分开写: 比如管理后台一个项目, 然后接口一个, 然后页面一个, 其实这样做也可以,但是这么做, 无 ...
- HTML的display属性将行内元素、块状元素、行内块状元素互相转换以及三者的区别
1.行内元素 查看演示 (1)设置宽高无效 (2)对margin仅设置左右方向有效,上下无效:padding设置上下左右都有效,即会撑大空间 (3)不会自动进行换行 <html> < ...
- JavaScript中赋值运算符的使用
JavaScript中的赋值运算可以分为简单赋值运算和复合赋值运算.简单赋值运算是将赋值运算符(=)右边表达式的值保存到左边的变量中:而复合赋值运算混合了其他操作(例如算术运算操作)和赋值操作.例如: ...
- 教材配套PPT初稿
1-10章初稿,基本完整.有些粗糙,后面可能会稍作调整. 附更新情况如下: 1.增加了第10章内容: 2.第5章增加了一些内容: 3.第3章内容部分更新: 4.增加了第8-9章内容. 订正:更新了第8 ...