SpringBoot集成接口文档swagger
程序员文章站
2022-04-14 23:17:53
...
1、引入依赖
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、编写配置文件
package com.newcapec.swaggerdemo.configuration;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* swagger配置类
* 访问地址:http://localhost:8077/swagger-ui.html#/
* 作者: 彭赛赛
* 创建时间:2019/12/27 11:29
*/
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.newcapec.swaggerdemo.controller"))//可修改的内容
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("我是标题:彭赛赛") //可修改的内容
.description("我是描述:彭赛赛管理平台")//可修改的内容
.version("1.0")//可修改的内容
.build();
}
}
3、编写一个controller
package com.newcapec.swaggerdemo.controller;
import java.util.ArrayList;
import java.util.List;
import com.newcapec.swaggerdemo.configuration.MyConfig;
import com.newcapec.swaggerdemo.model.Person;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* hello
* 作者: 彭赛赛
* 创建时间:2019/12/26 13:59
*/
@RestController
@Api(tags = "测试模块")
public class Hello {
@Autowired
private MyConfig myConfig;
@ApiOperation(value = "我是hello方法的value",notes ="我是hello方法的notes",response = String.class)
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
System.out.println(myConfig.getUsername() + ":" + myConfig.getPassword() + ":" + myConfig.getAge());
return "hello";
}
@ApiOperation(value = "getPersons的value" , notes = "getPersons的notes", response = List.class)
@RequestMapping(value = "/getpersons",method = RequestMethod.GET)
public List<Person> getPersons(){
ArrayList<Person> list = new ArrayList<>();
list.add(new Person(1,"深圳"));
list.add(new Person(2,"朝阳"));
list.add(new Person(3,"赛赛"));
return list;
}
}
4、启动项目,输入地址(在配置文件的注释中)即可查看接口文档
5、相关注解
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
6、踩坑记录
- 方法名一定要指定请求方式,否则会为一个方法生成所有的请求方式
- 接口一定是RESTfull风格,否则测试接口的时候不会起作用
- 未完待续……
上一篇: 电商项目(十六)--缓存系统
推荐阅读
-
Django使用swagger生成接口文档
-
.NET Core利用swagger进行API接口文档管理的方法详解
-
SpringBoot(六) SpringBoot整合Swagger2(自动化生成接口文档)
-
Springboot集成接口文档swagger,使用,测试
-
SpringBoot集成Swagger UI从零搭建 + 配置 + 使用说明
-
SpringBoot集成Swagger2构建在线API文档的代码详解
-
SpringBoot集成Spring security JWT实现接口权限认证
-
.netcore Swagger 生成 api接口文档
-
Springboot整合SwaggerAPI文档接口
-
SpringBoot项目使用Swagger2接口工具