今天我们来学习Swagger,这是个什么玩意呢?
Swagger可以根据配置,生成controller文档
众所周知,我是个干PHP开发的,我的所有文档都是手写的,大概是下边这个样子:
SpringBoot可以配置使用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.5.0</version>
</dependency>
二:编写Swagger的配置类
在config目录下创建SwaggerConfig.java文件,具体文件位置如下图所示:
代码如下所示:
package com.example.demo.demos.web.config;
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 SwaggerConfig {
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.demos.web.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SwaggerDemo")
.description("api描述")
.version("1.0")
.contact(new Contact("camellia","https://guanchao.site","805795955@qq.com"))
.build();
}
}
在上面的代码中,@Configuration 表示该类是一个配置类,@EnableSwagger2 表示启用 Swagger。在 api() 方法中,我们通过 `select() 方法配置扫描的包路径,paths() 方法配置接口的访问路径,apiInfo() 方法配置接口文档的相关信息。
三:在controller中配置
在编写接口时,我们需要使用 Swagger 的注解来描述接口信息。常用的注解包括:
1. @Api:用于描述接口的类或接口
2. @ApiOperation:用于描述接口的方法
3. @ApiParam:用于描述接口的参数
4. @ApiModel:用于描述数据模型
5. @ApiModelProperty:用于描述数据模型的属性
我们就简单来配置一下:
package com.example.demo.demos.web.controller;
import com.example.demo.demos.web.dao.User;
import com.example.demo.demos.web.dao.UserDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Api("用户管理控制器")
@Controller
public class UserController
{
/**
* 定义一个静态常量日志对象
*/
private static final Logger logger =LoggerFactory.getLogger(UserController.class);
// 自动装载
@Autowired
private UserDao userDao;
@ApiOperation("获得所有用户")
@RequestMapping("selectByExample")
@ResponseBody
public List<User> selectByExample()
{
// 输出日志信息
logger.info("selectByExample,info:");
logger.trace("selectByExample,trace:");
logger.debug("selectByExample,debug:");
logger.warn("selectByExample,warn:");
logger.error("selectByExample,error:");
List<User> list = userDao.selectByExample(null);
return list;
}
@ApiOperation("测试方法")
@RequestMapping("testsss")
@ResponseBody
public int test()
{
int list = (int)userDao.countByExample(null);
return list;
}
@GetMapping("/user/{id}")
@ApiOperation(value = "根据 ID 获取用户信息")
public User getUserById(@ApiParam(value = "用户 ID", required = true) @PathVariable Long id) {
// 根据 ID 查询用户信息
return null;
}
}
我们来访问:http://localhost:7099/swagger-ui.html,显示页面404
至今未找到问题。
有尝试过好用的小伙伴麻烦留言帮助下。
以上大概就是配置swagger的一次失败的小尝试。
最后,需要注意的是,Swagger 只是一种规范和工具集,它并不能取代单元测试和集成测试等其他测试方式。在开发过程中,我们需要综合使用各种测试方式,保证软件的质量和稳定性。
有好的建议,请在下方输入你的评论。