目录
5.启动项目,访问http://localhost:8080/swagger-ui.html#/
1.因为swagger实例的bean是Docket,所以我们通过配置一个Docket来配置swagger。
Swagger是什么?
Swapper号称是世界上最流行的api的框架。
官网:API Documentation & Design Tools for Teams | Swagger
Swagger的作用
编写代码的同时更新api文档,解决前后端的交流问题。
Spring boot 继承 swagger
1.建立一个spring boot项目,添加web依赖。
2.添加swagger依赖。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3.新建一个controller类
package cn.tedu.swagger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/user")
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
4.添加一个swagger配置类
package cn.tedu.swagger;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration //配置类交给spring管理
@EnableSwagger2 //开启swagger
public class SwaggerConfig {
}
5.启动项目,访问http://localhost:8080/swagger-ui.html#/
成功后如图所示:
Swagger配置
1.因为swagger实例的bean是Docket,所以我们通过配置一个Docket来配置swagger。
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2);
}
2.配置apiInfo()属性来配置文档信息
1.配置如下所示
private ApiInfo apiInfo() {
Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
return new ApiInfo(
"Swagger学习", // 标题
"配置Swagger", // 描述
"v7.77", // 版本