SpringBoot整合swagger2
1.使用 Swagger2 可以减少编写过多的文档,只需要通过代码就能生成文档API,提供给前端人员常方便。
在Java类中添加Swagger的注解即可生成Swagger接口,常用Swagger注解如下:
@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述 @ApiModel:用对象来接收参数 @ApiModelProperty:用对象接收参数时,描述对
象的一个字段 @ApiResponse:HTTP响应其中1个描述 @ApiResponses:HTTP响应整体描述 @ApiIgnore:使用
该注解忽略这个API @ApiError :发生错误返回的信息 @ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
@ApiImplicitParam属性:

例如:
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "path", dataType = "int"),
@ApiImplicitParam(name = "size", value = "每页记录数", required = true, paramType = "path", dataType = "int")
})
2.Swagger2依赖
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
3.代码
package com.qingfeng.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
@EnableSwagger2
public class Swagger2 {
// 配置swagger2核心配置 docket
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
.apiInfo(apiInfo()) // 用于定义api文档汇总信息
.select()
.apis(RequestHandlerSelectors
.basePackage("com.imooc.controller")) // 指定controller包
.paths(PathSelectors.any()) // 所有controller
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("某某平台接口api") // 文档页标题
.contact(new Contact("imooc",
"https://www.baidu.com",
"123456xxx@163.com")) // 联系人信息
.description("某某平台提供的api文档") // 详细信息
.version("1.0.1") // 文档版本号
.termsOfServiceUrl(https://www.baidu.com"") // 网站地址
.build();
}
}
4.启动你的项目,访问地址
两个都可以,推荐使用第二个 http://localhost: 端口号/swagger-ui.html 原路径 http://localhost:端口号/doc.html 原路径
5.访问的也面试英文的,我们可以在Controller里面进行中文优化
package com..qingfeng.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
//@ApiIgnore //忽略 就是这个Controller的所有的方法都不会显示在Swagger2的网页上
@RestController
@Api(value = "Hello接口controller", tags = {"Hello接口相关的api"})
public class HelloController {
@ApiOperation(value = "发送Hello", notes = "发送Hello", httpMethod = "GET") //在Swagger2页面方法的详细
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public Object hello(){
return "Hello World";
}
}
6.我们还可已在实体类里面优化
package com.qingfeng.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "用户对象", description = "从客户端,由用户传入的数据封装在此entity中")
public class User {
// example = "root" 是一个示例
@ApiModelProperty(value = "用户名", name = "username", example = "root", required = true)
private String username;
@ApiModelProperty(value = "密码", name = "password", example = "123456", required = true)
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

浙公网安备 33010602011771号