栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 配置使用swagger2操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 配置使用swagger2操作

swagger是一个功能强大的在线API文档的框架,提供了优雅的API在线文档的查阅和测试功能。

利用swagger2可以很方便的构建RESTful风格的API文档,在springboot中使用也非常方便,主要是在controller前配置添加注解就可以了,详细配置过程如下:

1. maven依赖包

使用目前最新版本为例,pom.xml添加的代码如下

  
    
      io.springfox
      springfox-swagger-ui
      2.9.2
    
    
    
      io.springfox
      springfox-swagger2
      2.9.2
    

2. 配置类的编写

配置类的编写同样非常简单,可以直接复制粘贴以下代码,但是一定要注意做适当修改,尤其是设置basePackage的路径,一定要根据实际情况修改。

新建一个config文件夹,在此文件夹中新建一个类

package cn.smileyan.swagger.config;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configurable
public class Swagger2 {
  
  @Bean
  public Docket createRestApi() {
    return new Docket(documentationType.SWAGGER_2)
 .apiInfo(apiInfo())
 .select()
 .apis(RequestHandlerSelectors.basePackage("cn.smileyan.swagger.controller"))
 .paths(PathSelectors.any())
 .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("springboot使用swagger例子")
 .description("简单优雅的restful风格")
 .termsOfServiceUrl("https://smileyan.cn")
 .version("1.0")
 .build();
  }
}

不能忘记类前面的@EnableSwagger2 与 @Configurable配置注解。以及后面的@Bean注解。

3. @EnableSwagger2 不能忘了

除了这个位置需要添加这个注解,还有springboot的运行类(application类)也要添加这个注释,否则会出现错误。

如图所示,我的application类名为SwaggerApplication,在这个类上面添加@EnableSwagger2

package cn.smileyan.swagger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class SwaggerApplication {

  public static void main(String[] args) {
    SpringApplication.run(SwaggerApplication.class, args);
  }
}

4. 编写controller类,添加注解,注意这个controller路径与上面配置类的路径要保持一致。

package cn.smileyan.swagger.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

  @ApiOperation(value = "用户测试",notes = "贵宾用户")
  @RequestMapping(value = "",method = RequestMethod.GET)
  private Map getUser() {
    Map map = new HashMap<>(1);
    map.put("result","success");
    return map;
  }
}

5. 运行,打开api文档http://localhost:8080/swagger-ui.html

效果如下:

可以点开user-controller,效果如下:

完成测试。很简单吧。

常用注解

@Api : 修饰整个类,用于描述Controller类

@ApiOperation:描述类的方法,或者说一个接口

@ApiParam:单个参数描述

@ApiModel:用对象来接收参数

@ApiProperty:用对象接收参数时,描述对象的一个字段

@ApiResponse:HTTP响应的一个描述

@ApiResponses:HTTP响应的整体描述

@ApiIgnore:使用该注解,表示Swagger2忽略这个API

@ApiError:发生错误返回的信息

@ApiParamImplicit:一个请求参数

@ApiParamsImplicit:多个请求参数

以上这篇springboot 配置使用swagger2操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131321.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号