SwaggerUi就是自动生成接口文档的这么一个类似于插件的工具,可以直接访问接口。

首先打开pom文件,将插件引进来,然后增加一个属性<properties>,用来设置版本号的,然后直接用${}引用。

<?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">
<parent>
<artifactId>JavaInterfaceTest</artifactId>
<groupId>com.peixm.code</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>Chapter10</artifactId> <properties>
<swagger.version>2.6.1</swagger.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
</dependencies> </project>

然后创建一个config包,在创建一个类SwaggerConfig.java,用来配置swager

package com.course.config;

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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration //在springboot里面专门为了加载配置文件的标签
@EnableSwagger2 //自动加载配置文件
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.paths(PathSelectors.regex("/.*")) //匹配那些访问的方法
.build();
} private ApiInfo apiInfo() {
//http://localhost:8888/swagger-ui.html
return new ApiInfoBuilder().title("我的接口文档")
.contact(new Contact("xiaomin","","553238711@qq.com"))
.description("这是我的swaggerui生成的接口文档")
.version("1.0.0.0")
.build();
} }

然后在想要在swagger看到的接口类的类名上添加注解:@Api(value = "/",description = "这是我全部的get方法"),在每个方法上面添加  @ApiOperation(value = "通过这个方法可以获取到cookies的值",httpMethod ="GET"),(或者post)value就是一个描述,描述这个方法是做什么的。

package com.course.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects; @RestController //被告诉我是你需要扫描的类
@Api(value = "/",description = "这是我全部的get方法")
public class MyGetMethod { @RequestMapping(value = "/getCookies",method = RequestMethod.GET) //访问的路径是什么
@ApiOperation(value = "通过这个方法可以获取到cookies的值",httpMethod ="GET")
public String getCookies(HttpServletResponse response){
//HttpServerletRequest 装请求信息
//HttpServerletResponse 装响应信息
Cookie cookie = new Cookie("login","ture");
response.addCookie(cookie); return "恭喜你获得cookies信息成功";
} /**
* 要求客户端携带cookies访问
* */ @RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
@ApiOperation(value = "要求客户端携带cookies访问",httpMethod = "GET")
public String getWithCookies(HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if(Objects.isNull(cookies)){
return "你必须携带cookies信息来";
} for(Cookie cookie : cookies){
if(cookie.getName().equals("login") && cookie.getName().equals("true")){
return "恭喜你访问成功";
}
}
return "你必须携带cookies信息来";
} /**
* 开发一个需要携带参数才能访问的get请求
* 第一种实现方式是 url: ip:port/get/with/param?key=value&key=value
* 模拟获取商品列表 开始页数,结束的页数,一页20条数据
* */ //第一种需要携带参数访问的get请求
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
@ApiOperation(value = "携带参数才能访问的get请求",httpMethod = "GET")
public Map<String,Integer> getList(@RequestParam Integer start,
@RequestParam Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("衬衫",300);
myList.put("干脆面",1); return myList; } /**
*第2种需要携带参数访问的get请求
* url: ip:port/get/with/param/10/20
* */ @RequestMapping(value = "/get/with/param/{start}/{end}")
@ApiOperation(value = "第2种需要携带参数访问的get请求",httpMethod = "GET")
public Map myGetList(@PathVariable Integer start,
@PathVariable Integer end){ Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("衬衫",300);
myList.put("干脆面",1); return myList;
} }

然后改变启动文件里面的要检测的包

然后在浏览器输入:http://localhost:8888/swagger-ui.html 就会出现所有的接口

点击接口可以进行接口测试:try out就可以请求

Springboot 4.Springboot 集成SwaggerUi的更多相关文章

  1. Springboot+swagger2.7集成开发

    Springboot+swagger2.7集成开发 本篇文章是介绍最新的springboot和swagger2.7集成开发和2.0稍微有一些出入: Springboot集成环境配置 Swagger2. ...

  2. 【java框架】SpringBoot(3) -- SpringBoot集成Swagger2

    1.SpringBoot web项目集成Swagger2 1.1.认识Swagger2 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体 ...

  3. SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL

    1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...

  4. SpringBoot(七):集成DataSource 与 Druid监控配置

    绑定DataSource:Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource,Druid是Java语言中最好的数据库连接池,并且能够提供 ...

  5. DEMO: springboot 与 freemarker 集成

    直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改. 1.pom.xml 中引用 依赖 <dependency> <groupId>org.s ...

  6. Springboot 和 Mybatis集成开发

    Springboot 和 Mybatis集成开发 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 jdk:1.7.0_79 maven:3.3.9 额外功能 PageHel ...

  7. 微服务学习三:springboot与springcloud集成之Eurake的使用(server端,client端)

    这个多亏了网站上的一个大神的博客: http://blog.csdn.net/forezp/article/details/70148833 强烈推荐学习: 1.springcloud是什么,这个大家 ...

  8. 在前后端分离的SpringBoot项目中集成Shiro权限框架

    参考[1].在前后端分离的SpringBoot项目中集成Shiro权限框架 参考[2]. Springboot + Vue + shiro 实现前后端分离.权限控制   以及跨域的问题也有涉及

  9. SpringBoot系列之集成jsp模板引擎

    目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...

  10. SpringBoot系列之集成Druid配置数据源监控

    SpringBoot系列之集成Druid配置数据源监控 继上一篇博客SpringBoot系列之JDBC数据访问之后,本博客再介绍数据库连接池框架Druid的使用 实验环境准备: Maven Intel ...

随机推荐

  1. Python之--paramiko实例

    一.基于SFTPClient类连接sshd服务器: 特点: 一般用于实现对远程服务器的上传, 下载和对远程目录文件的操作 import pramiko hostname = '172.24.0.110 ...

  2. mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享

    摘要: 下文讲述清空数据库中所有表信息的方法分享,如下所示: 实验环境:sql server 2008 实现思路: 1.禁用所有约束,外键 2.禁用所有触发器 3.删除表数据 4.开启触发器 5.开启 ...

  3. sqlserver数据库触发器调用外部exe

    sqlserver数据库触发器调用外部exe,同事可以选择参入参数! sqlserver使用 master..xp_cmdshell 进行外部exe的执行. 使用master..xp_cmdshell ...

  4. Linux学习历程——Centos 7 tar命令

    一.命令介绍 tar命令用于对文件进行打包压缩或解压. tar常用参数 参数 作用 -c 创建压缩文件 -x 解开压缩文件 -t 查看压缩包内有哪些文件 -r 向压缩归档末尾追加文件 -u 更新压缩包 ...

  5. zabbix监控Oracle

    可监控项 使用zabbix监控oracle数据库需要借助第三方的插件,目前使用较多的是orabbix.目前维护到了1.2.3版本.关于oracle自带的监控项目有以下几个: DB Version (i ...

  6. LeetCode算法题-Average of Levels in Binary Tree(Java实现)

    这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...

  7. Windows Service 学习系列(三)——循环引擎 ICycleEngine

    摘要:转载:https://www.cnblogs.com/zhuweisky/archive/2009/09/01/1557792.html#undefined 1.缘起: 有些系统需要每隔一段时间 ...

  8. 阿里巴巴开源的Asynchronous I/O Design and Implementation

    Motivation I/O access, for the most case, is a time-consuming process, making the TPS for single ope ...

  9. 初识shell编程

    1.shell编程之为什么学.怎么学 为什么学shell编程 Linux系统批量管理 提升工作效率,减少重复工作 学好shell编程所需要的基础知识 熟悉使用vim编辑器 熟悉SSH终端 熟练掌握Li ...

  10. (八)Index and Query a Document

    Let’s now put something into our customer index. We’ll index a simple customer document into the cus ...