0.引言及注意事项

Swagger是一个接口文档工具,依照Swagger可以0配置开发接口。不过要注意,Swagger是基于SpringBoot1.47版本开发的,而SpringBoot现在基本都是是2+。

如果要选用restful支持,只能将SpringBoot退出到1+版本。


1.maven引入

<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.Swagger配置文档

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class swaggerConfig {
@Bean
Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfoBuilder().description("项目").build());
}
}

3.接口配置

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.just.computer.mathproject.Entity.Advertisement;
import org.just.computer.mathproject.Service.AdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
@Api(tags ="广告")
@RequestMapping("/Advertisement/")
public class AdvertisementController {
@Autowired
AdvertisementService advertisementService; @ApiOperation(value ="获得所有广告")
@GetMapping("/getAllAdvertisement")
public List<Advertisement> getAllAdvertisement(){
return advertisementService.getAllAdvertisement();
}
@ApiOperation(value = "添加广告")
@GetMapping("/addAdvertisement")
public Boolean getAllAdvertisement(@RequestParam String img, @RequestParam String href){
try {
advertisementService.addAdvertisement(img,href);
return true;
}catch (Exception e){
return false;
}
} @ApiOperation(value = "删除广告")
@GetMapping("/deleteAdvertisement")
public Boolean deleteAdvertisementById(Integer id){
try{
advertisementService.deleteAdvertisementById(id);
return true;
}catch (Exception e){
return false;
}
}
}

本地运行后访问这里即可访问

或者到ip:端口/swagger-ui.html

运行结果如下

最新文章

  1. 关于VS2015支持编译Linux程序的问题
  2. JavaScript浮动广告代码,容纯DIV/CSS对联漂浮广告代码,兼容性非常好的js右下角与漂浮广告代码
  3. SQL Server AlwaysOn
  4. 关于freeCAD
  5. 【Linux_Fedora_应用系列】_2_如何安装视频播放器和视频文件解码
  6. 解决数据库datatime数据在DataGridView里不显示秒的解决
  7. OpenJudge/Poj 2105 IP Address
  8. the partition number
  9. Spreadsheet Calculator 电子表格计算器 (Uva 215)
  10. MYSQL数据备份与还原学习笔记
  11. C++中的struct与class继承方式
  12. little bird
  13. 三分钟明白 Activiti工作流 -- java运用
  14. java - day007 - 继承(2), 多态,面向对象,抽象类
  15. Python 文件行数读取的三种方法
  16. python(leetcode)-344反转字符串
  17. loadrunner&#160;脚本录制-Action分类
  18. C#编写的服务程序启动服务时报错误1053
  19. IntelliJ: Maven projects need to be imported: Import Changes Enable Auto-Import
  20. jsfl 巧用获取jsfl绝对路径,导入配置文件,注意配置文件无法改变舞台宽高

热门文章

  1. hive的metatable学习
  2. [20190530]ORACLE 18c - ALTER SEQUENCE RESTART.txt
  3. VMware Tools安装方法
  4. 关于ML.NET v1.0 的发布说明
  5. C学习笔记(5)--- 指针第二部分,字符串,结构体。
  6. 13.Java基础_数组内存图
  7. CSS自定义字体的实现,前端实现字体压缩
  8. cookiecutter
  9. TestNG.xml 配置
  10. 【Spring JDBC】JdbcTemplate(三)