前言

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

一、修改pom.xml文件,加入依赖

  1. <!--swagger-->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.2.2</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger-ui</artifactId>
  10. <version>2.2.2</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.codehaus.jackson</groupId>
  14. <artifactId>jackson-core-asl</artifactId>
  15. <version>1.9.13</version>
  16. </dependency>

二、添加Swagger配置类

  1. package com.slp.util;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import springfox.documentation.service.ApiInfo;
  6. import springfox.documentation.spi.DocumentationType;
  7. import springfox.documentation.spring.web.plugins.Docket;
  8. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  9.  
  10. import static springfox.documentation.builders.PathSelectors.regex;
  11.  
  12. /**
  13. * Created by sangliping on 2017/8/17.
  14. */
  15. @Configuration
  16. @EnableSwagger2
  17. public class Swaggers {
  18.  
  19. @Bean
  20. public Docket swaggerSpringMvcPlugin() {
  21. ApiInfo apiInfo = new ApiInfo("sample of springboot", "sample of springboot", null, null, null, null, null);
  22. Docket docket = new Docket(DocumentationType.SWAGGER_2).select().paths(regex("/user/.*")).build()
  23. .apiInfo(apiInfo).useDefaultResponseMessages(false);
  24. return docket;
  25. }
  26.  
  27. /*private ApiInfo apiInfo() {
  28. return new ApiInfoBuilder().title("测试API")
  29. .description("测试API1")
  30. .version("1.0.0")
  31. .build();
  32. }*/
  33. /* @Bean
  34. public Docket createRestApi() {
  35. return new Docket(DocumentationType.SWAGGER_2)
  36. .apiInfo(apiInfo())
  37. .select()
  38. .apis(RequestHandlerSelectors.basePackage("com.slp.web"))
  39. .paths(regex("/user/.*"))
  40. .build();
  41. }
  42. */
  43.  
  44. }

三、编写测试用的Controller类

  1. package com.slp.controller;
  2.  
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiImplicitParam;
  5. import io.swagger.annotations.ApiImplicitParams;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RestController;
  11.  
  12. /**
  13. * Created by sangliping on 2017/8/17.
  14. * Swagger常用注解:
  15. * @Api:修饰整个类,描述Controller的作用
  16. * @ApiOperation:描述一个类的一个方法,或者说一个接口
  17. * @ApiParam:单个参数描述
  18. * @ApiModel:用对象来接收参数
  19. * @ApiProperty:用对象接收常熟市,描述对象的一个字段
  20. * @ApiIgnore:使用该注解时忽略这个API
  21. * @ApiResponse:HTTP响应其中一个描述
  22. * @ApiResponses:HTTP响应整个描述
  23. */
  24. @RestController
  25. @RequestMapping("/user")
  26. @Api(value = "Shop")
  27. public class SpringBootController {
  28. @ApiOperation(value = "获取helloworld",notes = "简单的Spring boot请求")
  29. @RequestMapping
  30. String home (){
  31. return "HELLO WORLD";
  32. }
  33.  
  34. @ApiOperation(value = "获得参数",notes = "根据参数中的classNo和studentName是的字符串相加")
  35. @ApiImplicitParams({@ApiImplicitParam(name = "classNo",value ="班级编号",required= true,dataType="String")})
  36. @RequestMapping(value="/class/{classNo}/to/{studentName}",method = RequestMethod.GET)
  37. String world(@PathVariable("classNo")String classNo,@PathVariable("studentName")String studentName){
  38. return classNo + " "+studentName;
  39. }
  40.  
  41. }

四、Swagger启动

【Spring Boot&&Spring Cloud系列】Spring Boot项目集成Swagger UI的更多相关文章

  1. 15、Spring Boot 2.x 集成 Swagger UI

    1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...

  2. 项目集成swagger,并暴露指定端点给swagger

    项目集成swagger 一:思考: 1.swagger解决了我们什么问题? 传统开发中,我们在开发完成一个接口后,为了测试我们的接口,我们通常会编写单元测试,以测试我们的接口的可用性,或者用postm ...

  3. Maven + SpringMVC项目集成Swagger

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服 ...

  4. ASP.NET Core 2.2 WebApi 系列【四】集成Swagger

    Swagger 是一款自动生成在线接口文档+功能测试功能软件 一.安装程序包 通过管理 NuGet 程序包安装,搜索Swashbuckle.AspNetCore 二.配置 Swagger 将 Swag ...

  5. MVC项目集成swagger

    1.创建WebAPI项目解决方案 2.使用nuget引入Swashbuckle包 引入Swashbuckle包后App_Start文件夹下会多出一个SwaggerConfig文件 3.添加接口注释 项 ...

  6. spring boot 2.x 系列 —— spring boot 实现分布式 session

    文章目录 一.项目结构 二.分布式session的配置 2.1 引入依赖 2.2 Redis配置 2.3 启动类上添加@EnableRedisHttpSession 注解开启 spring-sessi ...

  7. spring boot 2.x 系列 —— spring boot 整合 redis

    文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...

  8. spring boot 2.x 系列 —— spring boot 整合 dubbo

    文章目录 一. 项目结构说明 二.关键依赖 三.公共模块(boot-dubbo-common) 四. 服务提供者(boot-dubbo-provider) 4.1 提供方配置 4.2 使用注解@Ser ...

  9. spring boot 2.x 系列 —— spring boot 整合 druid+mybatis

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构 项目查询用的表对应的建表语句放置在resour ...

随机推荐

  1. CentOS 7下彻底卸载MySQL数据库

    转载: https://zhangzifan.com/centos-7-remove-mysql.html

  2. Launchpad图标大小怎么调整?

    一.首先运行“终端”程序,执行以下命令: 1.调整每一列显示图标数量,7 表示每一列显示7个 defaults write com.apple.dock springboard-rows -int 7 ...

  3. Maven初步踩坑

    2015-02-08 今天创建maven项目,要从中央仓库下载一堆包到本地仓库,等了好久.结果下好了之后,maven项目上有个感叹号,也没有发现代码里哪配置有错误. 和实验室好多小伙伴一起交流 也没找 ...

  4. Java Web入门必知

    Java Web应用的核心技术是Java Server Page和Servlet.此外,开发一个完整的Java Web应该涉及一下几种概念及技术. 1.Servlet组件           Serv ...

  5. [转]十个 iOS 面试问题

    原文地址:http://onevcat.com/2013/04/ios-interview/ 不管对于招聘和应聘来说,面试都是很重要的一个环节,特别对于开发者来说,面试中的技术问题环节不仅是企业对应聘 ...

  6. Loadrunner对https协议(单双向SSL)的web端性能测试

    1.项目背景 1.1 单双向SSL的含义及部署 单向SSL即我们说到的https协议. 特点是,浏览器需要请求验证服务器证书: 基本含义是:一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务 ...

  7. sql server 2005 怎么看是不是企业版

    已经安装了SQL server 2005 怎么确定它是企业版 还是标准版??? 执行sql:SELECT @@VERSION在结果里看,比如我这里是(里面有Enterprise Edition,表示企 ...

  8. Android 安全提示 笔记

    http://developer.android.com/training/articles/security-tips.html1.数据存储内部存储internal storage存储的数据,只能由 ...

  9. sql server 2014 序列號

    亲测可用 27HMJ-GH7P9-X2TTB-WPHQC-RG79R

  10. linux将标准输出和标准错误输出都重定向到一个文件?

    需求描述: 今天在写crontab,里面有标准输出和错误输出,之前使用的是 > /dev/null 2>&1 那这个意思也就等同于将标准输出和错误输出都输出到/dev/null中, ...