1.先导入 pom文件
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.添加一个配置swagger的配置类
package com.ghc.test.config;
import org.springframework.beans.factory.annotation.Value;
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 SwaggerAppConfig {
@Value("${swagger.title}")
private String title;
@Value("${swagger.basepackage}")
private String basePackage;
@Value("${swagger.version}")
private String version;
@Value("${swagger.description}")
private String description;
@Value("${swagger.url}")
private String url;
@Value("${swagger.contact}")
private String contact;
@Bean
public Docket getDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApi())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
}
public ApiInfo getApi() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.termsOfServiceUrl(url)
.version(version)
.contact(new Contact(contact,url,url))
.build();
}
}
3.springboot配置文件
server:
port: 8080
swagger:
basepackage: com.ghc.test
title: Spring Boot 使用 Swagger2 构建RESTful API
version: v1.0
description: crud接口
url: http://www.east.net/
contact: ghc
4.controller中的相关接口
http://localhost:8080/swagger-ui.html
@ApiModel(description = "测试对象模型")
@Data
public class TestClass {
@ApiModelProperty(value="id" ,required=true)
private Integer id;
@ApiModelProperty(value="姓名" ,required=true)
private String name;
@ApiModelProperty(value="年龄" ,required=true)
private Integer age;
}
##使用map接受参数
@ApiOperation(value = "获取数据列表", notes = "根据查询条件获取数据列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "名称", paramType = "query",dataType ="String"),
@ApiImplicitParam(name = "age", value = "年龄", paramType = "query", dataType ="String")
})
@GetMapping
Object getList(@ApiIgnore @RequestParam Map param)
@ApiOperation(value = "获取数据列表", notes = "根据查询条件获取数据列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "名称", paramType = "query",dataType ="String"),
@ApiImplicitParam(name = "age", value = "年龄", paramType = "query", dataType ="Integer")
})
@GetMapping
public Object getList(@ApiIgnore @ModelAttribute TestClass entity) {
return entity;
}
@ApiOperation(value = "获取列表", notes = "根据查询条件获取列表数据")
@ApiImplicitParam(name = "entity", value = "测试实体类", paramType = "body",dataType = "TestClass")
@PostMapping
getList(@RequestBody TestClass entity)
@ApiOperation(value = "获取数据详情", notes = "根据查询id获取数据详情")
@ApiImplicitParam(name = "id", value = "自增id", paramType = "path",required = true)
@GetMapping("/{id}")
getListById(@PathVariable("id") int id)
@ApiOperation(value = "添加数据", notes = "添加数据")
@ApiImplicitParam(name = "entity", value = "实体类", paramType = "body",dataType = "TestClass")
@PostMapping
add(@RequestBody TestClass entity)
@ApiOperation(value = "修改数据", notes = "根据id修改数据")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "自增id", paramType = "path", required = true),
@ApiImplicitParam(name = "entity", value = "实体类", paramType = "body", dataType = "TestClass")
}
)
@PutMapping("/{id}")
updateById(@PathVariable("id") int id, @RequestBody TestClass entity)
@ApiOperation(value = "删除数据", notes = "根据id删除数据")
@ApiImplicitParam(name = "id", value = "自增id", paramType = "path", required = true)
@DeleteMapping("/{id}")
delById(@PathVariable("id") Integer id)
@ApiOperation(value = "批量删除数据", notes = "根据id集合删除数据")
@ApiImplicitParam(name = "idList", value = "自增id集合", paramType = "body", required = true)
@DeleteMapping
delByIds(@RequestBody List<Integer> idList)
5.启动项目,并浏览 “http://localhost:8081/swagger-ui.html”
swagger_ui 截图
自己测试的,目前只使用了这几个,可能写的不全已有在更新。