本篇文章给大家带来的内容是关于springboot 集成 swagger的方法介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
什么是swagger
swagger 可以生成一个具有互动性的api控制台,开发者可以用来快速学习和尝试apiswagger 可以生成客户端sdk代码用于各种不同的平台上的实现swagger 文件可以在许多不同的平台上从代码注释中自动生成swagger 有一个强大的社区依赖导入
<!-- swagger --><dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger2</artifactid> <version>2.4.0</version></dependency><dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger-ui</artifactid> <version>2.4.0</version></dependency>
加入配置
swagger: title: 项目 api description: springboot 集成 swagger 项目 api version: 1.0 terms-of-service-url: http://www.baidu.com/ base-package: cn.anothertale.springbootshiro # 这一项指定需要生成 api 的包,一般就是 controller contact: name: taohan url: http://www.baidu.ccom/ email: 1289747698@qq.com
建立 swagger config
package cn.anothertale.springbootshiro.config.swagger;import lombok.getter;import lombok.setter;import org.springframework.boot.autoconfigure.condition.conditionalonclass;import org.springframework.boot.context.properties.configurationproperties;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;/** * description: swagger 配置中心 * * @author: taohan * @date: 2019年03月20日 * @time: 16:52 */@getter@setter@configuration@enableswagger2@conditionalonclass(enableswagger2.class)@configurationproperties(prefix = "swagger")public class swaggerconfig { /** * api 接口包路径 */ private string basepackage; /** * api 页面标题 */ private string title; /** * api 描述 */ private string description; /** * 服务条款地址 */ private string termsofserviceurl; /** * 版本号 */ private string version; /** * 联系人 */ private contact contact; @bean public docket api() { return new docket(documentationtype.swagger_2) .apiinfo(apiinfo()) .select() .apis(requesthandlerselectors.basepackage(basepackage)) .paths(pathselectors.any()) .build(); } private apiinfo apiinfo() { return new apiinfobuilder() .title(title) .description(description) .termsofserviceurl(termsofserviceurl) .version(version) .contact(contact) .build(); }}
通过注解标明 api
swagger 默认根据配置的包,扫描所有接口并生成对应的 api 描述和参数信息。
常用注解及对应属性如下:
@api (描述一个 api 类,标注在 controller 上)
value:url 的路径值tags:如果设置该值,value 的值会被覆盖description:api 的资源描述basepath:基本路径可以不设置produces:比如:application/json, application/xml 类似 requestmapping 对应属性consumes:比如:application/json, application/xmlauthorizations:高级特性认证时配置hidden:是否在文档中隐藏
@apioperation (用在 controller 方法上,说明方法的作用)
value:url 的路径值tags:如果设置该值,value 的值会被覆盖description:对 api 资源的描述basepath:基本路径可以不设置position:如果配置多个 api 想改变展示位置,可通过该属性设置response:返回的对象responsecontainer:这些对象是有效的 list、set、map,其他无效httpmethod:请求方式code:http 状态码,默认200extensions:扩展属性
@apiimplicitparams (用在 controller 方法上,描述一组请求参数)
value:apiimplicitparam 数组,见下一注解
@apiimplicitparam(描述一个请求参数)
name:参数名称value:参数值defaultvalue:参数默认值required:是否必须,默认 falseaccess:不过多描述example:示例
@apiresponses (描述一组响应)
value:apiresponse数组,见下一注解
@apiresponse (描述一个响应)
code:http 的状态码message:描述消息最后,可以在浏览器中输入 http://localhost:8080/swagger-ui.html 即可访问!
以上就是springboot 集成 swagger的方法介绍(附代码)的详细内容。