SpringBoot集成Swagger
swagger可以让我们很容易的进行前后端的交互测试
首先新建一个spirngboot项目,勾选组件时勾选Spring-Web
1.导入依赖
<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>
2.编写hello工程
package com.kk.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello swagger";
}
}
3.编写swagger配置类
package com.kk.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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 //表明该类是一个配置类
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment){
//我们有这样一个需求:希望Swagger在开发环境中,在正式环境时不能使用
Profiles profiles = Profiles.of("dev");
boolean flag = environment.acceptsProfiles(profiles);
System.out.println(flag);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("柯柯") //设置默认分组名
.enable(flag) //关闭或开启swagger
.select()
.apis(RequestHandlerSelectors.basePackage("com.kk.controller")) //指定扫描的包
// .paths(PathSelectors.ant("/kk/**")) //不扫描该路径
.build();
}
//配置多个分组
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("AAA");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("BBB");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("CCC");
}
public ApiInfo apiInfo(){
return new ApiInfo(
"柯柯的大爹",
"很多小可爱",
"v1.1",
"https://www.cnblogs.com/qqkkOvO/",
new Contact("大王叫我来巡山", "https://www.cnblogs.com/qqkkOvO/", "446783737@qq.com"),//作者信息
"Apache2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>()
);
}
}
访问localhost:8080/swagger-ui.html,进入如下界面
swagger的默认示例:
自定义配置完以后:
4.配置AIP文档
-
实体类,
@ApiModel
为类添加注释,@ApiModelProperty
为类属性添加注释package com.kk.pojo; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @ApiModel("用户实体类") @AllArgsConstructor @NoArgsConstructor public class User { @ApiModelProperty("用户id") private int id; @ApiModelProperty("用户姓名") private String name; @ApiModelProperty("生日") @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8") private Date birth; }
-
控制器类,
@Api
为类添加注释,@ApiOperation
为方法添加注释package com.kk.controller; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.kk.pojo.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import java.util.Date; @RestController @RequestMapping("/hhh") @Api(tags="哈喽控制类") public class HelloController { /** 写一个hello测试工程 */ @ApiOperation("hello控制类") @GetMapping("/hello") public String helloCon() { return "hello swagger"; } /** 只要我们的接口返回值中存在实体类,就会自动被扫描到swagger的Model中 */ @ApiOperation("hello控制类") @GetMapping("/user1") public User user1() { return new User(999, "大宝贝", new Date("1997/12/17")); } @PostMapping("/user2") public User user2() { return new User(999, "大宝贝",new Date("1997/12/17")); } }
效果如图: