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