博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 生成接口文档 swagger2
阅读量:2386 次
发布时间:2019-05-10

本文共 5414 字,大约阅读时间需要 18 分钟。

swagger,中文“拽”的意思。它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试。

另外swagger很容易构建restful风格的api,简单优雅帅气,正如它的名字。

引入依赖

io.springfox
springfox-swagger2
2.8.0
io.springfox
springfox-swagger-ui
2.8.0

写配置类

import org.springframework.beans.factory.annotation.Value;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;@Configuration@EnableSwagger2public class SwaggerConfig {      Boolean swaggerEnabled = true;    @Bean    public Docket createRestApi() {        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                // 是否开启                .enable(swaggerEnabled)//true                .select()                .apis(RequestHandlerSelectors.basePackage("com.user.controller"))                .paths(PathSelectors.any())                .build();    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                //页面标题                .title(" 测试使用 ")                //创建人                .contact(new Contact("clc", "http://www.baidu.com", ""))                //版本号                .version("1.0")                //描述                .description("API 描述")                .build();    }}

通过@Configuration注解,表明它是一个配置类,

@EnableSwagger2开启swagger2。

apiINfo()配置一些基本的信息。apis()指定扫描的包会生成文档。

写生产文档的注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

  • @Api:修饰整个类,描述Controller的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiModel:用对象来接收参数
  • @ApiProperty:用对象接收参数时,描述对象的一个字段
  • @ApiResponse:HTTP响应其中1个描述
  • @ApiResponses:HTTP响应整体描述
  • @ApiIgnore:使用该注解忽略这个API
  • @ApiError :发生错误返回的信息
  • @ApiParamImplicitL:一个请求参数
  • @ApiParamsImplicit 多个请求参数

现在通过一个栗子来说明:

package clc.user.controller;import clc.user.bean.User;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import springfox.documentation.annotations.ApiIgnore;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;/** * ClassName: TestSwaggerController
* Description:
* date: 2018/11/30 10:06 AM
* * @author chengluchao * @since JDK 1.8 */@Api(tags = "用户API")@RestControllerpublic class TestSwaggerController { Map
users = Collections.synchronizedMap(new HashMap
()); @ApiOperation(value = "获取用户列表", notes = "获取用户列表") @RequestMapping(value = {"/getUser1"}, method = RequestMethod.GET) public List
getUser() { List
user = new ArrayList<>(users.values()); return user; } @ApiOperation(value = "创建用户", notes = "创建用户") @ApiImplicitParam(name = "user", value = "用户详细实体", required = true, dataType = "User") @RequestMapping(value = "/createUser", method = RequestMethod.POST) public String postUser(@RequestBody User user) { users.put(user.getAge(), user); return "success"; } @ApiOperation(value = "获用户细信息", notes = "根据url的id来获取详细信息") @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long", paramType = "path") @RequestMapping(value = "/byid", method = RequestMethod.GET) public User getUser(@PathVariable Long id) { return users.get(id); } @ApiOperation(value = "更新信息", notes = "根据url的id来指定更新用户信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path"), @ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "User") }) @RequestMapping(value = "/putid", method = RequestMethod.PUT) public String putUser(@PathVariable Long id, @RequestBody User user) { User user1 = users.get(id); user1.setName(user.getName()); users.put(id, user1); return "success"; } @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除用户") @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path") @RequestMapping(value = "/delid", method = RequestMethod.DELETE) public String deleteUser(@PathVariable Long id) { users.remove(id); return "success"; } @ApiIgnore//使用该注解忽略这个API @RequestMapping(value = "/hi", method = RequestMethod.GET) public String jsonTest() { return " hi you!"; }}

 

posted @
2019-05-23 11:06 阅读(
...) 评论(
...)

转载地址:http://sujab.baihongyu.com/

你可能感兴趣的文章
php memcached使用中的坑
查看>>
php变量引用和计数_refcount_gc和is_ref_gc
查看>>
windows环境下php和Php扩展编译,扩展dll文件编译
查看>>
magento 验证码
查看>>
magento性能优化系列二:db篇
查看>>
Discuz!$_G变量的使用方法
查看>>
magento memcache缓存配置
查看>>
PHP json_encode中文乱码解决方法
查看>>
mysql服务启动、关闭
查看>>
php获取中文字符串的首字符拼音字母
查看>>
php curl通过代理获取数据
查看>>
6 个 Linux性能监控命令行工具
查看>>
mysql 编码字符集配置
查看>>
php查看opcode编码的扩展 opdumper
查看>>
php转换html格式为文本格式
查看>>
mysql-proxy主从服务架构下读写分离和负载均衡实现及原理
查看>>
Nginx location 和 rewrite retry
查看>>
基于nginx的FastCGI的缓存配置
查看>>
Nginx模块fastcgi_cache的几个注意点
查看>>
PHP使用curl伪造IP地址和header信息
查看>>