Springboot+swagger2.7集成开发
Springboot+swagger2.7集成开发
本篇文章是介绍最新的springboot和swagger2.7集成开发和2.0稍微有一些出入:
- Springboot集成环境配置
- Swagger2.7集成
- 集成测试
Springboot集成环境配置
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。 —— [ INFO深入学习]
代码块
开发Spring Boot应用,例如:
@RestController
class App {
@RequestMapping("/")
String home() {
"hello"
}
}
引入SpringBoot的JAR文件.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
编制Main.java
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@EnableAutoConfiguration
public class Example { @RequestMapping("/")
String index() {
return "Hello World!";
} @RequestMapping("/index/{sayHello}")
String index(@PathVariable String sayHello) {
return "Hello "+sayHello+"!!!";
}
}
编写application.properties
application.hellowmsg=Hello World
server.port=8081 #端口
logging.level.=INFO
编制Application.java
@EnableScheduling
@EnableTransactionManagement
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(App.class);
app.setWebEnvironment(true);
app.setShowBanner(false);
Set<Object> set = new HashSet<Object>();
// set.add("classpath:applicationContext.xml");
app.setSources(set);
app.run(args);
}
运行Application.java
看到
2017-11-15 16:14:08.391 INFO 12524 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2017-11-15 16:14:08.607 INFO 12524 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
并没有出错,表示成功,直接访问http://localhost:8081/,输出Hello World!
Swagger2.7集成
加入maven的仓库文件
<!--swagger2 integration -->
<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>
创建SwaggerConfig的配置文件
import static com.google.common.collect.Lists.newArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.classmate.TypeResolver;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
@ConfigurationProperties
public class SwaggerConfig { @Value("${swagger.version}")
private String version;
@Autowired
private TypeResolver typeResolver; @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.hp.ctrl"))
.paths(PathSelectors.any()).build().apiInfo(apiInfo());
} private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("构建RESTful APIs").description("更多请关注:http://my.csdn.net/elvishehai")
.license("The Apache License, Version 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html").termsOfServiceUrl("http://my.csdn.net/elvishehai")
.version(version).build();
}
}
下一步写REST的COTR
@Api(value = "API - SMSController")
@RestController
public class SmsController {
@ApiOperation(value = "测试服务", notes = "测试服务")
@ApiImplicitParam(name = "smsRequestBean", value = "用户详细实体SmsRequestBean", required = true, dataType = "SmsRequestBean")
@RequestMapping(value = "/post", method = RequestMethod.POST)
public String post(@RequestBody SmsRequestBean smsRequestBean) {
return "服务测试成功,你输入的参数为:" + smsRequestBean.getApplicationId();
}
@ApiOperation(value = "测试服务", notes = "测试服务", consumes = "application/json", produces = "application/json")
@RequestMapping(value = "/test/{input}", method = RequestMethod.GET)
@ResponseBody
public String getUser(@PathVariable("input") String input) {
return "服务测试成功,你输入的参数为:" + input;
}
}
- swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
- @Api:修饰整个类,描述Controller的作用
- @ApiOperation:描述一个类的一个方法,或者说一个接口
- @ApiParam:单个参数描述
- @ApiModel:用对象来接收参数
- @ApiProperty:用对象接收参数时,描述对象的一个字段
- @ApiResponse:HTTP响应其中1个描述
- @ApiResponses:HTTP响应整体描述
- @ApiIgnore:使用该注解忽略这个API
- @ApiError :发生错误返回的信息
- @ApiParamImplicitL:一个请求参数
- @ApiParamsImplicit 多个请求参数
运行application.java
在浏览器上输入http://localhost:8081/swagger-ui.html
可以看到上面,测试就成功。
在上图请求的页面中,我们看到user的Value是个输入框?是的,Swagger除了查看接口功能外,还提供了调试测试功能,我们可以点击上图中右侧的Model Schema(黄色区域:它指明了User的数据结构),此时Value中就有了user对象的模板,我们只需要稍适修改,点击下方“Try it out!”按钮,即可完成了一次请求调用!
此时,你也可以通过几个GET请求来验证之前的POST请求是否正确。
相比为这些接口编写文档的工作,我们增加的配置内容是非常少而且精简的,对于原有代码的侵入也在忍受范围之内。因此,在构建RESTful API的同时,加入swagger来对API文档进行管理,是个不错的选择。
+参考信息
http://swagger.io/ Swagger官方网站
Springboot+swagger2.7集成开发的更多相关文章
- Springboot 和 Mybatis集成开发
Springboot 和 Mybatis集成开发 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 jdk:1.7.0_79 maven:3.3.9 额外功能 PageHel ...
- springboot与swagger2的集成
springboot与swagger2的集成 1.出现的背景 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染变成了:前端渲染.先后端分离的形态,而前端和后端的唯一联系,变成了API接口: ...
- SpringBoot | 第十章:Swagger2的集成和使用
前言 前一章节介绍了mybatisPlus的集成和简单使用,本章节开始接着上一章节的用户表,进行Swagger2的集成.现在都奉行前后端分离开发和微服务大行其道,分微服务及前后端分离后,前后端开发的沟 ...
- 在线Online表单来了!JeecgBoot 2.1 版本发布——基于SpringBoot+AntDesign的快速开发平台
项目介绍 Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. ...
- springboot+swagger2
springboot+swagger2 小序 新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行 ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- springboot 实战之一站式开发体验
都说springboot是新形势的主流框架工具,然而我的工作中并没有真正用到springboot: 都说springboot里面并没有什么新技术,不过是组合了现有的组件而已,但是自己却说不出来: 都说 ...
- 微服务学习三:springboot与springcloud集成之Eurake的使用(server端,client端)
这个多亏了网站上的一个大神的博客: http://blog.csdn.net/forezp/article/details/70148833 强烈推荐学习: 1.springcloud是什么,这个大家 ...
- SpringBoot系列之集成jsp模板引擎
目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...
随机推荐
- java 基本语法(八) 数组(一) 数组的概述
* 1.数组的理解:数组(Array),是多个相同类型数据一定顺序排列的集合,并使用一个名字命名, * 并通过编号的方式对这些数据进行统一管理. * * 2.数组相关的概念: * >数组名 * ...
- Python 实现邮件发送功能(初级)
在我们日常项目中,会经常使用到邮件的发送功能,如何利用Python发送邮件也是一项必备的技能.本文主要讲述利用Python来发送邮件的一些基本操作. 本章主要包含知识点: 邮件发送原理简述即常用smt ...
- 【Nginx】如何按日期分割Nginx日志?看这一篇就够了!!
写在前面 Nginx是没有以日期格式作为文件名来存储的,也就是说,Nginx不像Tomcat,每天自动生成一个日志文件,所有的日志都是以一个名字来存储,时间久了日志文件会变得很大.这样非常不利于分析. ...
- 1731: [Usaco2005 dec]Layout 排队布局*
1731: [Usaco2005 dec]Layout 排队布局 题意: n头奶牛在数轴上,不同奶牛可以在同个位置处,编号小的奶牛必须在前面.m条关系,一种是两头奶牛距离必须超过d,一种是两头奶牛距离 ...
- 理解js中的几种设计模式
目录 工厂模式 构造函数模式 原型模式 组合使用构造函数模式和原型模式 动态原型模式 其它模式 工厂模式 function createPerson(name, age){ var o = new O ...
- 用CBrother脚本实现smtp协议发送一份邮件
用CBrother脚本实现smtp协议发送一份邮件 之前用CBrother脚本写了一个拯救“小霸王服务器”的程序,公司人用着都挺好用,但是有时候谁重启了服务器其他人不知道,造成了多人多次重启,每个人都 ...
- 什么?你正在学web自动化测试?那这些Selenium的基本操作你了解过吗?
在自动化测试中,我们都知道是通过定位元素来实现的,那么有时候我们定位元素定位不到是为什么呢? 1.页面出现了iframe 2.出现了新的窗口,没有实现句柄的切换 3.三种等待方式,没有选择其中之一来使 ...
- P5198 [USACO19JAN]Icy Perimeter S (洛谷) (水搜索)
同样是因为洛谷作业不会写…… 写(水)博客啦. 直接放题目吧,感觉放在代码框里好看点 Farmer John要开始他的冰激凌生意了!他制造了一台可以生产冰激凌球的机器,然而不幸的是形状不太规则,所以他 ...
- P2060 马步距离(洛谷)
我们无论遇到什么困难,都不要拖,微笑着面对他,战胜拖延的最好方法就是面对拖延. 今天又拖延了…… 早晨听完老师讲课,本想做一道题练练手的,结果因为懒,瘫了一上午.最后在固定的刷题时间去面对了这道题,然 ...
- 导出Telegram贴纸
如何导出Telegram的贴纸1.在Telegram中 @StickerSetBot 机器人2.输入 /newpack 开启机器人,会提示 OK now send me stickers or sti ...