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. 数据库之redis篇(1)—— redis数据库安装,简单使用

    简介 reids,由Salvatore Sanfilippo写的一个高性能的key-value数据库,并且它是非关系型数据库,也就是没有像mysql那样多表链接操作,并且它是是完全开源免费的,遵守BS ...

  2. c/c++ llinux epoll系列5 解除epoll_wait状态

    linux epoll系列5 解除epoll_wait状态 有时候会有解除epoll_wait状态的需求. 实现方法: 1,给执行epoll_wait的程序发signal. 2,使用sockpair. ...

  3. ThinkPHP中使用聚合查询去重求和

    我使用的是TP5.1 首先去model类里面设置failed条件: 想要的效果: 数据库展示: 代码: eturn self::alias('gr') ->join('gs_staff gs', ...

  4. gulp配置(编译压缩转码自动刷新注释全)

    参考自:http://www.sheyilin.com/2016/02/gulp_introduce/ 在原先基础上增加了less编译 es6转码资源地图等,修改了一部分的热刷新. gulpfile. ...

  5. Java 8 中为什么要引出default方法

    (原) default方法是java 8中新引入进的,它充许接口中除了有抽象方法以外,还可以拥用具有实现体的方法,这一点跟jdk8之前的版本已经完全不一样了,为什么要这样做呢? 拿List接口举例,在 ...

  6. C++笔记-并发编程 异步任务(async)

    转自 https://www.cnblogs.com/diysoul/p/5937075.html 参考:https://zh.cppreference.com/w/cpp/thread/lock_g ...

  7. iOS开发基础-KVC简单介绍

    一.KVC介绍 键值编码(Key Value Coding,KVC):使开发者不必进行任何操作就可以进行属性的动态读写. KVC操作方法由 NSKeyValueCoding 协议提供,而 NSObje ...

  8. js 移除数组元素

    //移除数组元素 Array.prototype.remove = function(val) { var index = this.indexOfArr(val); if (index > - ...

  9. Java里的不能与无用.

    不能获取参数名 , 导致函数的参数名无用. 在MyBatis的方法里. 参数名是无法反射得到的. 导致必须使用注解,指定参数名. 这样的话. 参数名就没有了意义.

  10. 深入剖析Redis系列:Redis数据结构与全局命令概述

    前言 Redis 提供了 5 种数据结构.理解每种数据结构的特点,对于 Redis 的 开发运维 非常重要,同时掌握 Redis 的 单线程命令处理 机制,会使 数据结构 和 命令 的选择事半功倍. ...