swagger2maven依赖_Maven + SpringMVC项目集成Swagger
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。
作用:
接口的文档在线自动生成。
功能测试。
下面通过实现一个web项目来演示Swagger的使用。
1. 新建SpringMVC项目
1.1 新建项目
新建基于maven的web项目,导入spring相关依赖如下
4.0.0
com.zang.xz
mySwagger
0.0.1-SNAPSHOT
war
mySwagger Maven Webapp
http://www.example.com
UTF-8
4.3.6.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.8.9
org.springframework
spring-core
${spring.framework.version}
org.springframework
spring-context
${spring.framework.version}
org.springframework
spring-webmvc
${spring.framework.version}
mySwagger
1.2 配置web.xml和spring-mvc.xml
web.xml
Archetype Created Web Application
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
spring-mvc
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
spring-mvc.xml
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
1.3 新建entity和controller测试
为求简便,这里不集成dao层,数据直接从controller中封装返回。
Product.java
packagecom.zang.xz.entity;public classProduct {private static final long serialVersionUID = 1L;/**ID*/
privateLong id;/**产品名称*/
privateString name;/**产品型号*/
privateString productClass;/**产品ID*/
privateString productId;publicLong getId() {returnid;
}public voidsetId(Long id) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getProductClass() {returnproductClass;
}public voidsetProductClass(String productClass) {this.productClass =productClass;
}publicString getProductId() {returnproductId;
}public voidsetProductId(String productId) {this.productId =productId;
}
@OverridepublicString toString() {return "Product [id=" + id + ", name=" + name + ", productClass="
+ productClass + ", productId=" + productId + "]";
}
}
ProductController.java
packagecom.zang.xz.controller;importjava.util.Arrays;importjava.util.List;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importcom.zang.xz.entity.Product;
@RestController
@RequestMapping(value= {"/product/"})public classProductController {
@RequestMapping(value= "/{id}", method =RequestMethod.GET)public ResponseEntityget(@PathVariable Long id) {
Product product= newProduct();
product.setName("空气净化器");
product.setId(1L);
product.setProductClass("filters");
product.setProductId("T12345");returnResponseEntity.ok(product);
}
}
测试
至此,创建了一个简单的基于SpringMVC的Web项目,并能对外提供REST风格的API接口。接下来,我们要整合SpringFox和SwaggerUI到该SpringMVC项目中去,使其对外接口文档化。
2. 集成Swagger
2.1 添加swagger相关jar包
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
此处swagger 的核心依赖使用springfox-swagger2,SpringFox已经可以代替swagger-springmvc, 目前SpringFox同时支持Swagger 1.2 和 2.0。
2.2 添加SwaggerConfig
packagecom.zang.xz.controller;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importspringfox.documentation.builders.ApiInfoBuilder;importspringfox.documentation.builders.RequestHandlerSelectors;importspringfox.documentation.service.ApiInfo;importspringfox.documentation.spi.DocumentationType;importspringfox.documentation.spring.web.plugins.Docket;importspringfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2public classSwaggerConfig {
@BeanpublicDocket api() {return newDocket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())//显示所有类//.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//只显示添加@Api注解的类
.build()
.apiInfo(apiInfo());
}privateApiInfo apiInfo() {return newApiInfoBuilder()
.title("开放接口API") //粗标题
.description("HTTP对外开放接口") //描述
.version("1.0.0") //api version
.termsOfServiceUrl("http://xxx.xxx.com")
.license("LICENSE") //链接名称
.licenseUrl("http://xxx.xxx.com") //链接地址
.build();
}
}
2.3 静态资源访问配置
上面引入的springfox-swagger-ui依赖为我们提供了静态资源访问的支持,通过访问他为我们提供的页面,可以直观的看出项目所开放的接口API。
要想访问该页面,还需要增加访问配置,方法有两种:
2.3.1 在spring-mvc.xml中增加配置
2.3.2 或者增加配置类WebAppConfig
packagecom.zang.xz.controller;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.EnableWebMvc;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvcpublic class WebAppConfig extendsWebMvcConfigurerAdapter {
@Overridepublic voidaddResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
3. 测试API接口
3.1 访问“项目地址/swagger-ui.html#/”查看api
访问 http://localhost:8090/mySwagger/swagger-ui.html#/出现如下界面,说明我们的swagger集成项目成功。
在参数中输入信息,可实现对接口的调用
但是单调的页面没有实现swagger作为API文档工具的作用,这需要我们通过注解在接口方法中配置。
3.2 通过注解生成API文档
常用注解如下:
常用注解:
@Api()用于类; 表示标识这个类是swagger的资源
@ApiOperation()用于方法; 表示一个http请求的操作
@ApiParam()用于方法,参数,字段说明; 表示对参数的添加元数据(说明或是否必填等)
@ApiModel()用于类 表示对类进行说明,用于参数用实体类接收
@ApiModelProperty()用于方法,字段 ;表示对model属性的说明或者数据操作更改
@ApiIgnore()用于类,方法,方法参数 ;表示这个方法或者类被忽略
@ApiImplicitParam() 用于方法 ;表示单独的请求参数
@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
@RestController
@RequestMapping(value= { "/product/"})//类上加@Api注解
@Api(value = "/ProductController", tags = "接口开放示例")public classProductController {
@RequestMapping(value= "/{id}", method =RequestMethod.GET)//方法上加ApiOpreation注解
@ApiOperation(value = "根据id获取产品信息", notes = "根据id获取产品信息", httpMethod = "GET", response = Product.class)public ResponseEntityget(@PathVariable Long id) {
Product product= newProduct();
product.setName("空气净化器");
product.setId(1L);
product.setProductClass("filters");
product.setProductId("T12345");returnResponseEntity.ok(product);
}
}
添加注解之后,访问swagger界面如下
3.3 其他方法测试
多增加几个测试方法
@RequestMapping(method =RequestMethod.POST)
@ApiOperation(value= "添加一个新的产品")
@ApiResponses(value= { @ApiResponse(code = 405, message = "参数错误") })public ResponseEntityadd(Product product) {return ResponseEntity.ok("SUCCESS");
}
@RequestMapping(method=RequestMethod.PUT)
@ApiOperation(value= "更新一个产品")
@ApiResponses(value= { @ApiResponse(code = 400, message = "参数错误") })public ResponseEntityupdate(Product product) {return ResponseEntity.ok("SUCCESS");
}
@RequestMapping(method=RequestMethod.GET)
@ApiOperation(value= "获取所有产品信息", notes = "获取所有产品信息", httpMethod = "GET", response = Product.class, responseContainer = "List")public ResponseEntity>getAllProducts() {
Product product= newProduct();
product.setName("七级滤芯净水器");
product.setId(1L);
product.setProductClass("seven_filters");
product.setProductId("T12345");returnResponseEntity.ok(Arrays.asList(product, product));
}
swagger界面为不同方法提供不同颜色显示,可在其中对各个接口进行测试
项目结构如下:
swagger2maven依赖_Maven + SpringMVC项目集成Swagger相关推荐
- SpringMvc之集成Swagger
1 搭建springmvc环境(此处省略) 2 导入额外需要的相关jar swagger-springmvc-1.0.0.jar swagger-models-1.0.0.jar swagger-co ...
- jeesite项目集成swagger
引用https://blog.csdn.net/opopopwqwqwq/article/details/75087748 1.下载swagger UI https://github.com/swag ...
- Springboot项目集成Swagger
官网:https://swagger.io 一.准备工作 1). 导入knife4j的maven坐标 <dependency><groupId>com.github.xiaoy ...
- MP实战系列(二)之集成swagger
其实与spring+springmvc+mybatis集成swagger没什么区别,只是之前写的太不好了,所以这次决定详细写. 提到swagger不得不提rest,rest是一种架构风格,里面有对不同 ...
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
- springboot快速集成swagger
今天技术总监说:小明,我们本次3.0改造,使用swagger2.0作为前后端分离的接口规范,它可以一键生成前后端的API,一劳永逸--小明:??? Spring Boot 框架是目前非常流行的微服务框 ...
- SpringBoot实战(八):集成Swagger
强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 前后端分离是现在系统的主流,前端人员更多专注于前端功能,后端人员更加关注后端极大提高开 ...
- SpringBoot15:集成Swagger Api问题
项目集成Swagger 学习目标: 了解Swagger的概念及作用掌握在项目中集成Swagger自动生成API文档Swagger简介 前后端分离 前端 -> 前端控制层.视图层后端 -> ...
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】
项目结构: 1.maven项目的pom中引入shiro所需的jar包依赖关系 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
最新文章
- php扩展xdebug安装以及用kcachegrind系统分析
- 电脑出现kernelbase.dll错误的两种解决方法
- 2016最佳温情小说:雨还在下....
- 达内android 代码,深圳达内Android网页源码查看器.pdf
- vs2015配置boost c++
- 10.【Axure 10 】菜单-文件-导入RP文件
- matlab或_Matlab下载安装教程
- 地图学相关知识(二)
- 没有寻线仪怎么找网线_乱七八糟的网线怎么找?寻线仪来帮你
- 正确的计算机锁屏方法快捷键,电脑锁屏快捷键?(电脑快速锁屏以及酷炫快速切换窗口的方法!)...
- 微软良心伙伴,OneDrive首发支持iOS11的文件App
- 计算机网络的时间,计算机网络时间同步技术原理介绍
- Qt Opengl 给两个立方体设置两中不同颜色
- Symmetric diffeomorphic image registration with cross-correlation
- 阿里巴巴淘系技术部拍卖部-春招提前批
- 商业级、工业级、军品级、宇航级CPU有着不同标准
- 《Python语言程序设计》王恺 王志 机械工业出版社 第五章 序列、集合和字典 课后习题答案
- win10系统下配置maven环境
- 怎么用计算机管理权限删除文件,管理员权限删除文件方法介绍
- dede织梦模板目录大全,绝对有帮助
热门文章
- 自定义控件添加属性_|AutoCAD LT 2019 Mac自定义功能区的方法
- crontab 日志_liunx 中定时清理过期日志文件
- angularjs 元素重复指定次数_[LeetCode] 442. 数组中重复的数据
- PUC的完整形式是什么?
- 结语|日拱一卒无有尽,功不唐捐终入海
- 第 5-3 课:线程池——Executors + 面试题
- 面试突击第 3 期 | Redis 如何实现查询附近的人?视频实战版
- Oracle 日常巡检——数据库基本情况检查
- 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。
- ubuntu下创建软件图标和直接点文件打开