Springboot集成Swagger3
需要使用到哪些依赖呢?
项目pom.xml中下添加swagger-annotations和springfox-boot-starter
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.20</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
有哪些配置文件呢?
两个配置类Swagger3Configuration 和SwaggerProperties
import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;
@EnableOpenApi
@Configuration
public class Swagger3Configuration {
//http://localhost:8083/cloud-test/swagger-ui/index.html 地址
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("swagger")
public class SwaggerProperties {
/**
* 是否开启swagger,生产环境一般关闭,所以这里定义一个变量
*/
private Boolean enable;
/**
* 项目应用名
*/
private String applicationName;
/**
* 项目版本信息
*/
private String applicationVersion;
/**
* 项目描述信息
*/
private String applicationDescription;
/**
* 接口调试地址
*/
private String tryHost;
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public String getApplicationName() {
return applicationName;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
public String getApplicationVersion() {
return applicationVersion;
}
public void setApplicationVersion(String applicationVersion) {
this.applicationVersion = applicationVersion;
}
public String getApplicationDescription() {
return applicationDescription;
}
public void setApplicationDescription(String applicationDescription) {
this.applicationDescription = applicationDescription;
}
public String getTryHost() {
return tryHost;
}
public void setTryHost(String tryHost) {
this.tryHost = tryHost;
}
}
application.yml有没有搞事情?
本人自己工程配置了port和context-path,大家按照自己的来
server:
port: 8083
servlet:
context-path: /cloud-test
hello world搞一个
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
@Api("test part")
public class HelloController {
@RequestMapping("/hello")
@ApiOperation(value = "hello", httpMethod = "GET", response = String.class)
public String sayHello() {
return "hello world!";
}
}
浏览器输入:http://localhost:8083/cloud-test/swagger-ui/index.html
如下