一 新建springboot web项目
二 导入jar包
<dependency> <groupId>io.springfoxgroupId> <artifactId>springfox-swagger2artifactId> <version>2.9.2version> dependency> <dependency> <groupId>io.springfoxgroupId> <artifactId>springfox-swagger-uiartifactId> <version>2.9.2version> dependency>
三编写mapper service 和controller
四 配置swagger 创建swaggerConfig配置类
五 访问 http://localhost:8080/swagger-ui.html
六 配置swagger信息
package cn.lgy.swagger.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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;
import java.util.Enumeration;
@Configuration//表示这是一个配置类
@EnableSwagger2//开启swagger
public class swaggerConfig {
@Bean
//创建Docket类并注入到容器中,可以配置多个Docket,谁开发的就谁配置,通过调用groupName("刘高阳")方法设置
public Docket docket(Environment environment){
// 判断当前项目环境,也就是是开发状态还是发布状态或时生产状态
boolean dev = environment.acceptsProfiles("dev");
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(this.apiInfo())//调用apiInfo()方法覆盖原有配置,具体看Docket类
.groupName("刘高阳")//设置你自己的配置
.enable(dev)//是否启动swagger 默认为true,不启动不能用.
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
//basePackage("cn.lgy.swagger.demo.controller") 配置指定要扫描的包
//any()扫描全部
//none()全不扫描
//withClassAnnotation(Controller.class)扫描类上的注解,参数是类注解的反射对象
//withMethodAnnotation(RequestMapping.class)扫描方法上的注解,参数是方法注解反射对象
.apis(RequestHandlerSelectors.basePackage("cn.lgy.swagger.demo.controller"))
//paths()过滤路径,只扫描带有kuang下面的路径
// .paths(PathSelectors.ant("/kuang/**"))
.build();
}
//配置swagger信息
private ApiInfo apiInfo() {
//作者信息
Contact DEFAULT_CONTACT = new Contact("刘高阳", "https://www.bilibili.com/", "[email protected]");
return new ApiInfo(
"Api 刘高阳",
"hello接口文档信息",
"1.0",
"https://www.bilibili.com/",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
七 实体类配置
User.java
package cn.lgy.swagger.demo.pojo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel("用户实体类")//用于说明这是个什么类 public class User { @ApiModelProperty("用户名")//用于说明这是个什么字段 public String username; @ApiModelProperty("密码") public String password; }
HelloController.java
package cn.lgy.swagger.demo.controller; import cn.lgy.swagger.demo.pojo.User; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.web.bind.annotation.*; @RestController public class HelloController { @GetMapping("hello") public String hello(){ return "hello word"; } @PostMapping("user") //只要我们的接口中,返回值中存在实体类,就会被扫描到swagger中 @ApiOperation("获取user对象")//也是用于注释该方法解释的 public User user(){ return new User(); } @PostMapping("user1") //只要我们的接口中,返回值中存在实体类,就会被扫描到swagger中 @ApiOperation("post测试")
@ApiParam("username")//用于解释参数的
public String user1(@ApiParam("username") String username){ return "hello"+username; } }