随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、前后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远。 
前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,swagger就是一款让我们更好地书写API文档的框架。swagger可以用来显示API文档。

  我们在基于spring框架进行java开发时,当需要使用swagger生成API文档时,需要先添加swagger的jar包依赖。我们以在maven项目管理工具中开发项目为例,在pom.xml中添加如下依赖:

<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.5</version>
</dependency> <dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.1</version>
</dependency>

  接着,要写一个swagger的核心配置类,如下所示:

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration//声明此类是一个swagger的核心配置类
@EnableSwagger//启用Swagger
@EnableWebMvc//启用SpringMVC public class SwaggerConfig { //给SpringSwaggerConfig进行赋值
private SpringSwaggerConfig springSwaggerConfig; @Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
this.springSwaggerConfig = springSwaggerConfig;
} /**
* 自定义实现 customImplementation
* @return
*/
@Bean //再配置一个SwaggerSpringMvcPlugin 的bean,将来SpringMVC生成文档时要用
public SwaggerSpringMvcPlugin customImplementation(){
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.includePatterns(".*?");
} /**
* title;
description;
terms of serviceUrl;
contact email;
license type;
license url;
* @return
*/
private ApiInfo apiInfo(){
ApiInfo apiInfo = new ApiInfo(
"XXX项目接口文档",
"企业版,为企业用车提供一站式解决方案:海量优质车型,实时调度,会议、商务用车尽享便捷;多种灵活用车方式,快车、专车、接送机一键完成",
"http://192.168.1.236",
"联系邮箱",
"许可证的类型",
"许可证的链接"
); return apiInfo;
}
}

  然后,在springmvc-config.xml配置文件里的扫描注解里加上@Configuration,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--springmvc 只管扫描 controller包-->
<context:component-scan base-package="com.itszt.XXX.controller,com.itszt.XXX.controller.swagger">
<context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
</context:component-scan> <!--支持注解->
<mvc:annotation-driven>
<!--让我们的Adapter有json 转换能力-->
<mvc:message-converters>
<ref bean="jsonconverter"></ref>
</mvc:message-converters>
</mvc:annotation-driven> <!--jackson给我们提供的一个json转换器-->
<bean id="jsonconverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!--静态资源的处理-->
<mvc:resources mapping="/pages/*" location="/htmls/"></mvc:resources>
<mvc:resources mapping="/swagger/**/*" location="/swagger/"></mvc:resources> <bean class="com.itszt.XXX.controller.swagger.SwaggerConfig"></bean>
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig"></bean> </beans>

  接下来,下载swagger- UI,添加到 SpringMVC 的静态文件的目录里。

  然后,在使用中,实体类上用注解@ApiModel(description = "返回的结果"),其下属性用注解@ApiModelProperty(notes = "注册失败的错误信息");

  控制器上用注解@Api(description="用户中心模块",value = "用户相关操作"),其下方法用注解@ApiOperation(notes = "验证用户接口",value = "用户登录使用的方
法",httpMethod = "GET"),方法的参数前用注解@ApiParam(name="username",required = true,value = "用户名")。

  最后,我们在浏览器地址栏里访问  http://localhost/api-docs,即可看到页面swagger包下的index.html动态显示的相应内容:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" />
<link href='css/typography.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='css/reset.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='css/reset.css' media='print' rel='stylesheet' type='text/css'/>
<link href='css/print.css' media='print' rel='stylesheet' type='text/css'/> <script src='lib/object-assign-pollyfill.js' type='text/javascript'></script>
<script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
<script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
<script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
<script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
<script src='lib/handlebars-4.0.5.js' type='text/javascript'></script>
<script src='lib/lodash.min.js' type='text/javascript'></script>
<script src='lib/backbone-min.js' type='text/javascript'></script>
<script src='swagger-ui.js' type='text/javascript'></script>
<script src='lib/highlight.9.1.0.pack.js' type='text/javascript'></script>
<script src='lib/highlight.9.1.0.pack_extended.js' type='text/javascript'></script>
<script src='lib/jsoneditor.min.js' type='text/javascript'></script>
<script src='lib/marked.js' type='text/javascript'></script>
<script src='lib/swagger-oauth.js' type='text/javascript'></script> <!-- Some basic translations -->
<!-- <script src='lang/translator.js' type='text/javascript'></script> -->
<!-- <script src='lang/ru.js' type='text/javascript'></script> -->
<!-- <script src='lang/en.js' type='text/javascript'></script> --> <script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
// url = "http://petstore.swagger.io/v2/swagger.json";
url = "http://localhost/api-docs";
} hljs.configure({
highlightSizeThreshold: 5000
}); // Pre load translate...
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
window.swaggerUi = new SwaggerUi({
url: url,
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function(swaggerApi, swaggerUi){
if(typeof initOAuth == "function") {
initOAuth({
clientId: "your-client-id",
clientSecret: "your-client-secret-if-required",
realm: "your-realms",
appName: "your-app-name",
scopeSeparator: " ",
additionalQueryStringParams: {}
});
} if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
},
onFailure: function(data) {
log("Unable to Load SwaggerUI");
},
docExpansion: "none",
jsonEditor: false,
defaultModelRendering: 'schema',
showRequestHeaders: false
}); window.swaggerUi.load(); function log() {
if ('console' in window) {
console.log.apply(console, arguments);
}
}
});
</script> </head> <body class="swagger-section">
<div id='header'>
<div class="swagger-ui-wrap">
<a id="logo" href="http://swagger.io"><img class="logo__img" alt="swagger" height="30" width="30" src="data:images/logo_small.png" /><span class="logo__title">swagger</span></a>
<form id='api_selector'>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
<div id='auth_container'></div>
<div class='input'><a id="explore" class="header__btn" href="#" data-sw-translate>Explore</a></div>
</form>
</div>
</div> <div id="message-bar" class="swagger-ui-wrap" data-sw-translate> </div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>

spring集成swagger的更多相关文章

  1. Spring 集成 Swagger UI

    <!-- Spring --> <dependency> <groupId>org.springframework.boot</groupId> < ...

  2. Spring集成Swagger,Java自动生成Api文档

    博主很懒... Swagger官网:http://swagger.io GitHub地址:https://github.com/swagger-api 官方注解文档:http://docs.swagg ...

  3. Spring集成swagger步骤(包含Header)

    1.添加依赖,2.4.0: <dependency> <groupId>io.springfox</groupId> <artifactId>sprin ...

  4. Spring Boot 集成 Swagger,生成接口文档就这么简单!

    之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...

  5. Spring Boot 集成Swagger

    Spring Boot 集成Swagger - 小单的博客专栏 - CSDN博客https://blog.csdn.net/catoop/article/details/50668896 Spring ...

  6. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  7. Spring Boot 项目实战(三)集成 Swagger 及 JavaMelody

    一.前言 上篇介绍了 Logback 的集成过程,总体已经达到了基本可用的项目结构.本篇主要介绍两个常用工具,接口文档工具 Swagger .项目监控工具 JavaMelody 的集成步骤. 二.Sw ...

  8. 在spring+springMvc+mabatis框架下集成swagger

    我是在ssm框架下集成swagger的,具体的ssm搭建可以看这篇博文: Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构 本项目的GitHu ...

  9. 【Swagger】可能是目前最好的 Spring Boot 集成 swagger 的方案

    [Swagger]可能是目前最好的Spring Boot集成 swagger 的方案 ![](https://img2018.cnblogs.com/blog/746311/201909/746311 ...

随机推荐

  1. 洛谷 P1023 税收与补贴问题 (2000NOIP提高组)

    洛谷 P1023 税收与补贴问题 (2000NOIP提高组) 题意分析 一开始没理解题意.啰啰嗦嗦一大堆.看了别人的题解才明白啥意思. 对于样例来说,简而言之: 首先可以根据题目推算出来 28 130 ...

  2. 【loj6179】Pyh的求和

    Portal -->loj6179 Solution ​  这题其实有一个式子一喵一样的版本在bzoj,但是那题是\(m\)特别大然后只有一组数据  这题多组数据== ​    首先根据\(\v ...

  3. python基础----元类metaclass

    1 引子 class Foo: pass f1=Foo() #f1是通过Foo类实例化的对象 python中一切皆是对象,类本身也是一个对象,当使用关键字class的时候,python解释器在加载cl ...

  4. python函数:字符串函数示例

    优先掌握的操作 #作用:名字,性别,国籍,地址等描述信息 #定义:在单引号\双引号\三引号内,由一串字符组成 name='egon' #优先掌握的操作: #1.按索引取值(正向取+反向取) :只能取 ...

  5. 杨辉三角之c实现任意行输出

    #include<stdio.h> #include<stdlib.h> int** fmalloc(int n){ int** array; //二维指针 int i; ar ...

  6. jquery validate ajax 验证重复的2种方法

    转载自:http://blog.51yip.com/jsjquery/1484.html jquery validate 经过这种多年的改良,已经很完善了.它能满足80%的验证需要,如果validat ...

  7. HDU 2619 完全剩余类 原根

    求有多少$i(<=n-1)$,使 $x^i  \mod n$的值为$[1,n-1]$,其实也就是满足完全剩余类的原根数量.之前好像在二次剩余的讲义PPT里看到这个过. 直接有个定理,如果模k下有 ...

  8. HDU 1402 FFT 大数乘法

    $A * B$ FFT模板题,找到了一个看起来很清爽的模板 /** @Date : 2017-09-19 22:12:08 * @FileName: HDU 1402 FFT 大整数乘法.cpp * ...

  9. CF544 C 背包 DP

    n个人写m行代码,第i人写一行代码有a[i]个bug,问总bug数不超过b的不同方案数. 其实就是个背包,dp[i][j][k]代表前i个人写了j行代码用了k个bug限度,然后随便转移一下就好了 /* ...

  10. [洛谷P3293] [SCOI2016]美味

    洛谷题目链接:[SCOI2016]美味 题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 ...