springboot+swagger(3.0.0版本)配置
文章目录
1.创建springboot项目,导入依赖
1.1)勾选spring web
1.2)导入swagger依赖(这里使用的是swagger 3.0版本)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
3.编写代码
3.1)controller
package com.qwl.swaggerdemo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorld {
@GetMapping("/helloWorld")
String helloWorld(){
return "helloWorld";
}
}
3.2)swagger的配置类
注释已经非常详细了,在最后有图文详解
package com.qwl.swaggerdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration//springboot启动时就加载该类
@EnableSwagger2//注明此类是swagger的配置类
public class SwaggerConfig {
//配置swagger的Docket的Bean
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//apiInfo信息
.select()
.apis(RequestHandlerSelectors.basePackage("com.qwl.swaggerdemo.controller"))//扫描指定包下的内容
.paths(PathSelectors.any())//设置路径筛选 any():满足条件的路径,该断言总为true
.build();
}
/**
*配置swagger的apiInfo信息
* swagger 3.0的访问地址:http://localhost:8080/swagger-ui/index.html
* @return
*/
private ApiInfo apiInfo(){
//作者
Contact contact = new Contact("齐威龙","https://www.baidu.com","291532919@qq.com");
return new ApiInfo(
"齐威龙Api文档",//标题
"随便写点描述",//描述
"qwl.1.0",//版本
"https://kaifa.baidu.com",//服务条款url
contact,//联系
"Apache 2.0",//许可
"http://www.apache.org/licenses/LICENSE-2.0",//许可url
new ArrayList());
}
}
4.启动项目,访问swagger
在swagger(3.0.0版本)中,url发生变化。
url:http://localhost:8080/swagger-ui/index.html