当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC和Swagger整合方法

SpringMVC和Swagger整合方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

蚂蚁的生活习性,利用线头编织的47种可爱小物件,末世兑换高手txt下载

描述

swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 restful 风格的 web 服务。

总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 api 来始终保持同步。swagger 让部署管理和使用功能强大的 api 从未如此简单。

配置

1、引入相关jar包:

<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、创建java配置类

@configuration
@enableswagger2
public class swagger2 {
  private apiinfo apiinfo() {
    return new apiinfobuilder()
         // 文档标题
        .title("wish")
        // 文档描述
        .description("https://github.com/handexing").termsofserviceurl("https://github.com/handexing")
        .version("v1")
        .build();
  }
  @bean
  public docket createrestapi() {
    return new docket(documentationtype.swagger_2)
        .apiinfo(apiinfo())
        .select()
        // 指定controller存放的目录路径
        .apis(requesthandlerselectors.basepackage("com.wish.controller"))
        .paths(pathselectors.any())
        .build();
  }
}

3、编写接口文档测试

@requestmapping(value = "testsawgger", method = requestmethod.post, produces = "application/json; charset=utf-8")
@apioperation(value = "测试swagger", httpmethod = "post", notes = "testsawgger")
public executeresult<boolean> adduser(@apiparam(value = "参数", required = true) long id) {
  executeresult<boolean> result = new executeresult<boolean>();
  try {
    result.setsuccess(true);
  } catch (exception e) {
    result.setsuccess(false);
  }
  return result;
}

说明:

@apioperation:用在方法之上

1、value: 表示接口名称

2、notes: 表示接口详细描述

3、httpmethod:表示接口请求方法类型

@apiparam:用在方法参数上

1、required:表示参数是否必须传

2、name:表示参数名称

3、value:表示参数描述

测试

swagger2文档的默认地址是 /swagger-ui.html, 本地开发的访问就可以看到自动生成的文档了

结语

到这就配置好了,最终demo可查看

总结

以上所述是小编给大家介绍的springmvc和swagger整合方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网