Skip to content

Commit

Permalink
添加springboot+swagger示例工程
Browse files Browse the repository at this point in the history
  • Loading branch information
mason committed Nov 13, 2019
1 parent 41e5fe0 commit ff532db
Show file tree
Hide file tree
Showing 18 changed files with 1,072 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

## `java-monitor-example`

用于java监测技术文章的示例说明,此示例是一个`spring boot`工程,里面包含简单的`controller``service`类,`OOM`的接口等功能。
用于[java监测技术文章](https://mianshenglee.github.io/)的示例说明,此示例是一个`spring boot`工程,里面包含简单的`controller``service`类,`OOM`的接口等功能。

## `springboot-swagger-demo`

用于[springboot+swagger接口文档企业实践](www.baidu.com)文章的示例说明,此示例是一个`spring boot` + `swagger`的工程,里面包含了对swagger的注解使用,参数,参数化,接口认证,接口过滤等功能。
31 changes: 31 additions & 0 deletions springboot-swagger-demo/hello-swagger-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

logs/
target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/


95 changes: 95 additions & 0 deletions springboot-swagger-demo/hello-swagger-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>hello-swagger-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Demo project for hello swagger</description>

<properties>
<java.version>1.8</java.version>
<springboot.version>2.2.0.RELEASE</springboot.version>
<swagger.version>2.7.0</swagger.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>${springboot.version}</version>
</dependency>

<!-- swagger接口 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- 可选界面,访问地址:docs.html-->
<!--<dependency>-->
<!--<groupId>com.github.caspar-chen</groupId>-->
<!--<artifactId>swagger-ui-layer</artifactId>-->
<!--<version>1.1.3</version>-->
<!--</dependency>-->
<!-- 可选界面,访问地址:doc.html-->
<!--<dependency>-->
<!--<groupId>com.github.xiaoymin</groupId>-->
<!--<artifactId>swagger-bootstrap-ui</artifactId>-->
<!--<version>1.7</version>-->
<!--</dependency>-->

<!-- 工具包:hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>4.5.7</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${springboot.version}</version>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.mason.helloswagger.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author mason
*
* @date 2019/11/1
*/
@SpringBootApplication
public class HelloSwaggerDemoApplication {

public static void main(String[] args) {
SpringApplication.run(HelloSwaggerDemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package me.mason.helloswagger.demo.config;

import cn.hutool.core.util.StrUtil;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* swagger 配置类,访问地址:http://ip:port/swagger-ui.html
*
* @author mason
*
* @date 2019/6/1
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Autowired
private SwaggerInfo swaggerInfo;

/**
* 默认分组全部接口
* @return
*/
@Bean
public Docket defaultApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否启用
.enable(swaggerInfo.getEnable());
ApiSelectorBuilder builder = docket.select();
//api过滤
builder = builder.apis(Predicates.or(apisFilter()));
//接口路径过滤
if (StrUtil.isNotEmpty(swaggerInfo.getAntPath())) {
builder = builder.paths(PathSelectors.ant(swaggerInfo.getAntPath()));
}

// //全局header参数
// ParameterBuilder tokenPar = new ParameterBuilder();
// List<Parameter> pars = new ArrayList<Parameter>();
// tokenPar.name("Authorization").description("token令牌")
// .modelRef(new ModelRef("string"))
// .parameterType("header")
// .required(true).build();
// pars.add(tokenPar.build());
// docket.globalOperationParameters(pars);

//使用认证上下文
docket.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
return builder.build();
}


/**
* 过滤不显示的接口
* @return 返回需要过滤的条件数组
*/
private List<Predicate<RequestHandler>> apisFilter() {
List<Predicate<RequestHandler>> apis = new ArrayList<>();
String basePackageStr = swaggerInfo.getBasePackage();
// 1.包过滤
if (StrUtil.isNotEmpty(basePackageStr)) {
//支持多个包
String[] basePackages = basePackageStr.split(";");
if (null != basePackages && basePackages.length > 0) {
Predicate<RequestHandler> predicate = input -> {
// 按basePackage过滤
Class<?> declaringClass = input.declaringClass();
String packageName = declaringClass.getPackage().getName();
return Arrays.asList(basePackages).contains(packageName);
};
apis.add(predicate);
}
}
return apis;
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder().title(swaggerInfo.getTitle())
.description(swaggerInfo.getDescription())
.version(swaggerInfo.getVersion())
.licenseUrl(swaggerInfo.getLicenseUrl())
.contact(new Contact(swaggerInfo.getContactName()
,swaggerInfo.getContactUrl()
,swaggerInfo.getContactEmail()))
.build();
}

private List<ApiKey> securitySchemes() {
return Lists.newArrayList(
new ApiKey("Authorization", "Authorization", "header"));
}
private List<SecurityContext> securityContexts() {
return Lists.newArrayList(
SecurityContext.builder()
.securityReferences(defaultAuth())
//正则式过滤,此处是所有非login开头的接口都需要认证
.forPaths(PathSelectors.regex("^(?!login).*$"))
.build()
);
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "认证权限");
return Lists.newArrayList(
new SecurityReference("Authorization", new AuthorizationScope[]{authorizationScope}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.mason.helloswagger.demo.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


/**
* swagger配置信息
* @author mason
* @date 2019/10/31
*/
@Component
@ConfigurationProperties(prefix = "swagger")
@PropertySource("classpath:/config/swagger.properties")
@Data
public class SwaggerInfo {
private String basePackage;
private String antPath;
private String title = "HTTP API";
private String description = "Swagger 自动生成接口文档";
private String version ;
private Boolean enable;
private String contactName;
private String contactEmail;
private String contactUrl;
private String license;
private String licenseUrl;
}
Loading

0 comments on commit ff532db

Please sign in to comment.