对于搬砖的同学来说,写接口容易,写接口文档很烦,接口变动,维护接口文档就更更更烦,所以经常能发现文档与程序不匹配。

等过一段时间就连开发者也蒙圈了

Swagger2快速方便的解决了以上问题。一个能与Spring MVC程序配合组织出强大RESTful API文档的新宠儿。

下面直接上代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.zhongxin.wealth</groupId>
<artifactId>wealthweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>wealthweb</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <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>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>
</dependencies> </project>

  创建配置类

package com.zhongxin.wealth.apiConfig;

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; /**
* Created by DingYS on 2017/12/8.
*/ @Configuration
@EnableSwagger2
public class Swagger2 { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zhongxin.wealth.web"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("廊坊委贷大数据统计结果输出接口")
.version("1.0")
.build();
} }

  controller编写

package com.zhongxin.wealth.web;

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; /**
* Created by DingYS on 2017/12/7.
*/
@RestController
@RequestMapping("/hello")
public class HelloWordController { @ApiOperation(value="测试接口", notes="这只是一个测试controller调用的接口,没有任何的业务逻辑")
@RequestMapping(value = {"/test"},method = RequestMethod.GET)
public String testHello(){
return "hello";
} }

  代码完成,准备看效果

点击Try it out!

是不是很详细,很高大上。

注:集成过程中刚开始用的swagger2.2.2版本,会在首页出现一个error的错误提醒

{“schemaValidationMessages”:[{“level”:”error”,”message”:”Can’t read from file http://127.0.0.1:8888/v2/api-docs"}]}

  但是浏览器访问:http://127.0.0.1:8888/v2/api-docs 又能获取 结果

{“swagger”:”2.0”,”info”:{“version”:”1.0”,”title”:”廊坊委贷大数据统计结果输出接口”,”contact”:{},”license”:{}},”host”:”127.0.0.1:8888”,”basePath”:”/“,”tags”:[{“name”:”hello-word-controller”,”description”:”Hello Word Controller”}],”paths”:{“/hello/test”:{“get”:{“tags”:[“hello-word-controller”],”summary”:”测试接口”,”description”:”这只是一个测试controller调用的接口,没有任何的业务逻辑”,”operationId”:”testHelloUsingGET”,”consumes”:[“application/json”],”produces”:[“/“],”responses”:{“200”:{“description”:”OK”,”schema”:{“type”:”string”}},”401”:{“description”:”Unauthorized”},”403”:{“description”:”Forbidden”},”404”:{“description”:”Not Found”}}}}}}

  具体原因本人不明,换成2.7.0版本以后没在出现。

springboot集成swagger的更多相关文章

  1. spring-boot 集成 swagger 问题的解决

    spring-boot 集成 swagger 网上有许多关于 spring boot 集成 swagger 的教程.按照教程去做,发现无法打开接口界面. 项目由 spring mvc 迁移过来,是一个 ...

  2. 20190909 SpringBoot集成Swagger

    SpringBoot集成Swagger 1. 引入依赖 // SpringBoot compile('org.springframework.boot:spring-boot-starter-web' ...

  3. SpringBoot集成Swagger,Postman,newman,jenkins自动化测试.

    环境:Spring Boot,Swagger,gradle,Postman,newman,jenkins SpringBoot环境搭建. Swagger简介 Swagger 是一款RESTFUL接口的 ...

  4. springboot集成swagger添加消息头(header请求头信息)

    springboot集成swagger上篇文章介绍: https://blog.csdn.net/qiaorui_/article/details/80435488 添加头信息: package co ...

  5. springboot 集成 swagger 自动生成API文档

    Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.简单来说,Swagger是一个功能强大的接口管理工具,并且提供了多种编程语言的前后端分离解决方案. S ...

  6. SpringBoot集成Swagger接口管理工具

    手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工具,比如postman 接口文档太多,不好管 ...

  7. springboot 集成swagger ui

    springboot 配置swagger ui 1. 添加依赖 <!-- swagger ui --> <dependency> <groupId>io.sprin ...

  8. springboot集成swagger实战(基础版)

    1. 前言说明 本文主要介绍springboot整合swagger的全过程,从开始的swagger到Knife4j的进阶之路:Knife4j是swagger-bootstarp-ui的升级版,包括一些 ...

  9. springboot集成swagger文档

    //此处省略springboot创建过程 1.引入swagger相关依赖(2个依赖必须版本相同) <dependency> <groupId>io.springfox</ ...

随机推荐

  1. WPF ListBox 一些小知识点

    页面代码: <Grid Grid.Row="0" Grid.Column="2"> <ListBox x:Name="lvStep& ...

  2. 小技巧:Oracle:sqlplus 显示行列字符数

    遇到这种情况可以判断:行显示字符数不够,可以增加行显示字符数 01.可以当前会话HR@ACE >set line 400; 02.上面的方法其它会话不生效,懒不想每次设置怎么办? Oracle: ...

  3. 微软Tech Summit 2017,等你来打Call

    2017年10月31至11月3日,由微软举办的Tech Summit 2017技术暨生态大会将在北京盛大举办,要在北京连开四天.今年的技术大会看头十足,不仅有大咖级人物带来十二大主题课程,更有三天四场 ...

  4. LAMP 实现全过程及wordpress的搭建

    一.介绍 1. LAM(M)P: L:linux A:apache (httpd) M:mysql, mariadb M:memcached 缓存 P:php, perl, python WEB 资源 ...

  5. Redis环境搭建

    一.准备的安装包 windows虚拟机软件:VMware Workstation Pro 12 linux安装文件:CentOS-7-x86_64-Minimal-1511.iso 远程登录软件:pu ...

  6. 关于安卓手机的牛逼软件termux使用

    最近在学着用linux,偶尔发现了一款神奇的软件--termux,termux是一款来自国外的终端模拟器,是运行在内部存储上的程序(不在内存卡上),功能比较强大,启动程序之后会进入命令行终端,需要基本 ...

  7. c++学习笔记---04---从另一个小程序接着说

    从另一个小程序接着说 文件I/O 前边我们已经给大家简单介绍和演示过C和C++在终端I/O处理上的异同点. 现在我们接着来研究文件I/O. 编程任务:编写一个文件复制程序,功能实现将一个文件复制到另一 ...

  8. What is npm?

    什么是npm ? npm全称是Node Package Manager npm makes it easy for JavaScript developers to share and reuse c ...

  9. 关于帧动画steps属性的理解

    CSS3的Animation有八个属性 animation-name animation-duration animation-delay animation-iteration-count anim ...

  10. chrony软件使用说明

    1.1.1 chrony简介 Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确. 它由两个程序组成:chronyd和chronyc. chronyd是一个后 ...