首先创建一个springboot的项目,不细说网上有
在进行整合swagger2的时候,引入swagger2的jar
io.springfox
springfox-swagger2
2.7
io.springfox
springfox-swagger-ui
2.7
引入swagger2的jar包之后,我们需要配置一个swagger2的配置类,来声明一些swagger2的配置信息
SwaggerConfig.java
import io.swagger.annotations.ApiOperation;
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;
/**
* Swagger2的接口配置
*
* @author Lion Li
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 创建API
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
// 详细定制
.apiInfo(apiInfo())
.select()
// 指定当前包路径 (扫描所有带ApiOperation注解的类)
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 扫描所有
//.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}/**
* 添加摘要信息
*/
private ApiInfo apiInfo() {
// 用ApiInfoBuilder进行定制
return new ApiInfoBuilder()
.title("标题:XXXX_接口文档")
.description("描述:XXX项目文档")
.contact(new Contact(项目名称, null, null))
.version("版本号:"+项目版本号)
.build();
}
}
生成后
文章图片
swagger2就已经配置完毕了。接下来你只需要在你的接口上配置你想要显示的信息即可。
关于其中@Api和@ApiOperation等的详细解释如下:
文章图片
关于参数的详细解释
文章图片
用在 @ApiResponses里边
下面是具体demo类
DemoController.java
/**
* 测试 控制层
*
* @author Lion Li
* @date 2019-04-02
*/
@Api(value = "https://www.it610.com/article/user",tags = "用户模块")
@Controller
@RequestMapping("/demo/demo")
public class DemoController {
private String prefix = "demo/demo";
private final IDemoService demoService;
@Autowired
public DemoController(IDemoService demoService) {
this.demoService = demoService;
} /**
*列表页
*/
@GetMapping
@ApiOperation(value = "https://www.it610.com/article/列表页")
public String demo(){
return prefix + "/demo";
} /**
* 新增页
*/
@GetMapping("/add")
@ApiOperation(value = "https://www.it610.com/article/新增页")
public String add(){
return prefix + "/add";
} /**
* 编辑页
*/
@GetMapping("/edit/{id}")
@ApiOperation(value = "https://www.it610.com/article/编辑页")
public String edit(@PathVariable("id") Integer id, ModelMap mmap){
Demo demo = demoService.getById(id);
mmap.put("demo", demo);
return prefix + "/edit";
}
/**
* 查询测试列表
*/
@PostMapping("/list")
@ResponseBody
@ApiOperation(value = "https://www.it610.com/article/获取demo列表")
public Result list(Demo demo){
startPage();
List list = demoService.list(demo);
return Result.success(getDataTable(list));
} /**
* 新增保存测试
*/
@PostMapping("/add")
@ResponseBody
@ApiOperation(value = "https://www.it610.com/article/添加demo案例")
public Result addSave(Demo demo){
demoService.save(demo);
return Result.success();
} /**
* 修改保存测试
*/
@PostMapping("/edit")
@ResponseBody
@ApiOperation(value = "https://www.it610.com/article/编辑demo")
public Result editSave(Demo demo){
demoService.updateById(demo);
return Result.success();
}
/**
* 删除测试
*/
@PostMapping( "/remove")
@ResponseBody
@ApiOperation(value = "https://www.it610.com/article/删除demo")
@ApiImplicitParam(name = "ids", value = "https://www.it610.com/article/需要删除的id串", required = true, dataType = "String")
public Result remove(String ids){
Long[] longIds = Convert.toLongArray(ids);
demoService.removeByIds(Arrays.asList(longIds));
return Result.success();
} /**
* 导出测试列表
*/
@PostMapping("/export")
@ResponseBody
@ApiOperation(value = "https://www.it610.com/article/导出demo")
public Result export(Demo demo){
List list = demoService.list(demo);
ExcelUtil util = new ExcelUtil<>(Demo.class);
util.exportExcel(list, "demo");
return Result.success();
}
}
生成后
文章图片
Demo.java实体类编写(这里采用了lombok,简化了实体类代码)
/**
* 测试表 demo_demo
*
* @author Lion Li
* @date 2019-04-02
*/
// swagger注解声明model
@ApiModel
// lombok注解(具体功能自行百度)
@EqualsAndHashCode(callSuper = true)
@ToString
@Data
@Accessors(chain = true)
// 数据库表名映射
@TableName("demo_demo")
public class Demo {
private static final long serialVersionUID = 1L;
@TableId
@ApiModelProperty(value = "https://www.it610.com/article/id")
private Integer id;
/**
* 键
*/
@ApiModelProperty(value = "https://www.it610.com/article/键",required = true)
private String key;
/**
* 值
*/
@ApiModelProperty(value = "https://www.it610.com/article/值",required = true)
private String value;
/**
* 创建时间
*/
@ApiModelProperty(value = "https://www.it610.com/article/创建时间")
private Date createtime;
}
生成后
【maven|springboot2整合swagger2自动生成文档】
文章图片
推荐阅读
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- jar|springboot项目打成jar包和war包,并部署(快速打包部署)
- 数据库|效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- java人生|35K 入职华为Java开发那天,我哭了(这 5 个月做的一切都值了)
- Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)
- 微服务|微服务系列:服务发现与注册-----Eureka(面试突击!你想了解的Eureka都在这里.持续更新中......)
- java|ApplicationListener和SpringApplicationRunListener的联系
- Spring|SpringSecurity--自定义登录页面、注销登录配置
- 性能|性能工具之 Jmeter 通过 SpringBoot 工程启动
- java|图解四种 IO 模型