<font color=#999AAA >
</font>
(文章目录)
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
前言
<font color=#999AAA > </font>
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
一、建立swagger公共模块
1.先建立公共模块下子模块common-swgger
为了之后复用及整合cloud项目,我是在cloud项目下建立的,具体结构如下:
其pom依赖如下:
<parent>
<artifactId>ams-common</artifactId>
<groupId>com.ams</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common-swagger</artifactId>
<dependencies>
<!--前3项swagger本篇章必须,其他为了之后使用-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
2.建立配置类
1.1 SwaggerConfig配置类
package com.ams.common.swagger.config;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* @ClassName SwaggerConfig
* @Desription swagger配置类
* @Author folyh
* @Version 1.0
**/
@Slf4j
@Configuration
@EnableOpenApi//开启swagger
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
// 当配置中存在swagger.enabled生效,matchIfMissing = true默认生效
public class SwaggerConfig {
@Autowired
private SwaggerProperties properties;
@Bean // 相当于Spring 配置中的<bean>
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact(
properties.getContact().getName(),
properties.getContact().getUrl(),
properties.getContact().getEmail()
);
return new ApiInfoBuilder()
.title(properties.getTitle())
.description(properties.getDescription())
.termsOfServiceUrl("http://www.baidu.com")
.contact(contact)
.version(properties.getVersion())
.build();
}
1.2 SwaggerProperties文件配置类
package com.ams.common.swagger.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName SwaggerProperrties
* @Desription swagger文件配置类
* @Author folyh
* @Version 1.0
**/
@Data
@Configuration
@ConfigurationProperties("swagger")
public class SwaggerProperties {
/**
* 是否开启swagger
*/
private Boolean enabled;
/**
* swagger会解析的包路径
**/
private String basePackage = "/**";
/**
* 标题
**/
private String title = "";
/**
* 描述
**/
private String description = "";
/**
* 版本
**/
private String version = "";
/**
* host信息
**/
private String host = "";
/**
* 联系人信息
*/
private Contact contact = new Contact();
@Data
public static class Contact {
/**
* 联系人
**/
private String name;
/**
* 联系人url
**/
private String url;
/**
* 联系人email
**/
private String email;
}
}
1.3 自动配置注入spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ams.common.swagger.config.SwaggerConfig,\
com.ams.common.swagger.config.SwaggerProperties
二、建立swagger测试模块
1.如图:
2.pom配置
1.1 ams-test模块pom配置
为了之后其他测试模块复用
<parent>
<artifactId>ams-cloud</artifactId>
<groupId>com.ams</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ams-test</artifactId>
<packaging>pom</packaging>
<modules>
<module>test-swagger</module>
</modules>
<dependencies>
<!-- 前2项为本swagger配置文件、web页面必用的-->
<!-- Spring Cloud & Alibaba -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>com.ams</groupId>
<artifactId>common-web</artifactId>
<version>${ams.version}</version>
</dependency>
<!-- 配置读取 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ams</groupId>
<artifactId>common-base</artifactId>
<version>${ams.version}</version>
</dependency>
<dependency>
<groupId>com.ams</groupId>
<artifactId>ams-common</artifactId>
<version>${ams.version}</version>
</dependency>
</dependencies>
3. 写好bootstrap.yml配置
server:
port: 22001
spring:
application:
name: ams-test-swagger
swagger:
enabled: true
title: test-swagger
description: "测试swagger"
version: 1.0.0
contact:
name: folyh
email: xxx@
url: http://baidu.com
4. 写测试类
1.1 先写启动类
package com.ams.test.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName TestSwaggerApp
* @Desription swagger测试
* @Author folyh
* @Version 1.0
**/
@SpringBootApplication
public class TestSwaggerApp {
public static void main(String[] args) {
SpringApplication.run(TestSwaggerApp.class, args);
}
}
1.2 写个TestMessageDto等下使用
package com.ams.test.swagger.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @ClassName TestSwaggerDto
* @Desription
* @Author kevin
* @Version 1.0
**/
@Data
@ApiModel("测试信息Dto")
public class TestMessageDto {
@ApiModelProperty(value = "信息内容",dataType = "String",name = "content",example = "丝袜哥come on!")
private String content;
@ApiModelProperty(value = "信息种类",dataType = "Integer",name = "type",example = "001")
private Integer type;
}
1.3 再写SwaggerController测试
package com.ams.test.swagger.controller;
import com.ams.common.base.result.R;
import com.ams.test.swagger.dto.TestMessageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
/**
* @ClassName SwaggerController
* @Desription 测试swagger控制器
* @Author folyh
* @Version 1.0
**/
@RestController
@RequestMapping("/testSwagger")
@Slf4j
@Api(value = "testSwagger",tags = "测试swagger")
public class SwaggerController {
@ApiIgnore //忽略这个api
@GetMapping("/hello")
public String hello(){
return "hello";
}
@ApiOperation(value = "测试swagger",notes = "打印信息")
@GetMapping(value = "/print/{message}")
@ApiImplicitParam(name = "message", value = "请传递要打印的信息",required = true, dataType = "String", paramType = "path")
public R print(@PathVariable String message){
log.info("测试swagger打印信息:"+message);
return R.ok("测试swagger打印信息:"+message);
}
@PostMapping(value = "/printMessage")
@ApiOperation(value = "测试swagger信息", notes = "打印实体信息(JSON格式)")
public R printMessage(@RequestBody TestMessageDto messageDto){
log.info("测试swagger打印实体信息,信息内容:"+messageDto.getContent()+";信息种类:"+messageDto.getType());
return R.ok("测试swagger打印实体信息:"+messageDto);
}
}
3.测试
1.1 运行项目,打开http://localhost:22001/swagger-ui/index.html,
如图:
1.2 分别测试两个接口,如图:
1.下期将讲解:springcloud整合swagger
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
总结
<font color=#999AAA > 随心所往,看见未来。Follow your heart,see night!<br/> 欢迎点赞、关注、留言,一起学习、交流!