第一步: 新建SpringBoot项目,引入依赖.
<!-- swagger2 依赖 -->
<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>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<!-- swagger2 依赖 -->
上面两个依赖的作用:
springfox-swagger2依然是依赖OSA规范文档,也就是一个描述API的json文件,而这个组件的功能就是帮助我们自动生成这个json文件,
springfox-swagger-ui就是将这个json文件解析出来,用一种更友好的方式呈现出来。
第二步:创建api
@RestController
@RequestMapping("/admin/index")
public class AdminIndexController{
@RequestMapping("/index",method = RequestMethod.GET)
public String index(){
return "this is index";
}
}
第三步:配置Swagger2
现在Swagger2还不能为我们生成API文档,因为我们还没有对它进行配置.
我们需要创建一个配置类,进行如下配置:
package com.wondream.myframework.admin.config;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger在线文档配置<br>
* 项目启动后可通过地址:http://host:ip/swagger-ui.html 查看在线文档
*
* @author enilu
* @version 2018-07-24
*/
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class AdminSwagger2Configuration {
@Bean
public Docket adminDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("admin")
.apiInfo(adminApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.wondream.myframework.admin.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo adminApiInfo() {
return new ApiInfoBuilder()
.title("litemall-admin API")
.description("litemall管理后台API")
.termsOfServiceUrl("https://github.com/linlinjava/litemall")
.contact("https://github.com/linlinjava/litemall")
.version("1.0")
.build();
}
}
搞定
现在我们要做的配置已经能满足一个生成API文档的基本要求了,让我们启动项目,访问:http://localhost:port/swagger-ui.html
Swagger2 注解使用
@RestController
@RequestMapping(value = "/user", produces = APPLICATION_JSON_VALUE) //配置返回值 application/json
@Api(description = "用户管理")
public class HelloController {
ArrayList<User> users = new ArrayList<>();
@ApiOperation(value = "获取用户列表", notes = "获取所有用户信息")
@RequestMapping(value = {""}, method = RequestMethod.GET)
public List<User> hello() {
users.add(new User("逻辑", "luoji"));
users.add(new User("叶文杰", "yewenjie"));
return users;
}
}
@ApiModelProperty(hidden = true)
private String id;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "密码")
private String password;
@ApiModelProperty(value = "邮箱")
private String email;
@ApiModelProperty(hidden = true)
private Integer age;
@ApiModelProperty(hidden = true)
private Boolean enabled;