每次修改完代码需要找原本的API时楼主的内心是痛苦的,因为一般情况下都找不到,需要重新写一份。如果使用Swagger的话,只要加几个注解就可以实时生成最新的在线API文档,而且不仅仅是文档,同时支持API接口的测试。下面呢,给大家分享一下Spring Boot 集成 Swagger 的步骤。

一、引入jar包

      <!-- Swagger2核心包-->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency> <!-- Swagger2 UI包,前端展示API文档 -->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

二、配置SwaggerConfig

 package com.bjgoodwill.oip.major.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.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; /**
* @Description: swagger配置文件
* @Date 2018/7/13 10:50
* @Author HQueen
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.queeen.major"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API文档")
.build();
}
}

其中,@EnableSwagger2注解来启用Swagger2,apis()定义了扫描的包路径

三、编写接口

Spring Boot中包含了一些注解,对应于HTTP协议中的方法:

@GetMapping对应HTTP中的GET方法;

@PostMapping对应HTTP中的POST方法;

@PutMapping对应HTTP中的PUT方法;

@DeleteMapping对应HTTP中的DELETE方法;

@PatchMapping对应HTTP中的PATCH方法。

 package com.bjgoodwill.oip.major.system.controller;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.bjgoodwill.oip.core.enums.ExceptionEnum;
import com.bjgoodwill.oip.core.exception.CoreException;
import com.bjgoodwill.oip.core.util.StringUtils;
import com.bjgoodwill.oip.major.system.model.UserModel;
import com.bjgoodwill.oip.major.system.service.UserService;
import com.bjgoodwill.oip.major.system.util.R; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; /**
* @Date 2018/7/13 10:50
* @Author HQueen
*/
@Controller
@RequestMapping(value="/user")
@Api(tags="用户信息")
public class UserController { @Autowired
UserService userService; /** 验证用户信息
* @param account
* @return
*/
@ApiOperation(value="验证用户信息", notes="验证用户信息")
@PostMapping(value="/verify")
@ResponseBody
public UserModel verifyUser(@ApiParam(name="account", value="用户名") String account){ if (StringUtils.isEmpty(account)) {
throw new CoreException(ExceptionEnum.REQUEST_NULL);
} UserModel model = userService.findByAccount(account); return model;
} /** 跳转至用户修护界面
* @param id
* @param model
* @return
*/
@ApiOperation(value="跳转至用户修护界面", notes="跳转至用户修护界面")
@GetMapping(value="/pre/{id}")
public String preUpdate(@ApiParam(name="id", value="用户ID") Long id, Model model) { UserModel userModel = userService.getByPrimaryKey(id); model.addAttribute("model", userModel); return "edit";
} /** 修改用户基本信息
* @param userModel
* @return
*/
@ApiOperation(value="修改用户基本信息", notes="修改用户基本信息")
@PostMapping(value="/update")
@ResponseBody
public R update(@ApiParam(name="userModel", value="用户信息类") UserModel userModel) { if (userService.update(userModel) > 0) {
return R.ok();
} return R.error();
}
}

四、启动及测试

  如上图所示,点击 Try it out! 就可以进行在线测试。

PS:

1. swagger和swagger 2的区别

  在官方文档上是这么说明的:

What is the relationship between swagger-ui and springfox-swagger-ui?

  • Swagger Spec is a specification.
  • Swagger Api - an implementation of that specification that supports jax-rs, restlet, jersey etc.
  • Springfox libraries in general - another implementation of the specification focused on the spring based ecosystem.
  • Swagger.js and Swagger-ui - are client libraries in javascript that can consume swagger specification.
  • springfox-swagger-ui - the one that you’re referring to, is just packaging swagger-ui in a convenient way so that spring services can serve it up.

  总的来说就是:

    1. Swagger 是一种规范。

    2. springfox-swagger 是基于 Spring 生态系统的该规范的实现。

    3. springfox-swagger-ui 是对 swagger-ui 的封装,使得其可以使用 Spring 的服务

2. 其他注解方式

  在上述 demo 中,楼主使用@ApiParam注解对参数进行描述,下面呢,楼主提供第二种注解方式。

 package com.bjgoodwill.oip.major.system.controller;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.bjgoodwill.oip.core.enums.ExceptionEnum;
import com.bjgoodwill.oip.core.exception.CoreException;
import com.bjgoodwill.oip.core.util.StringUtils;
import com.bjgoodwill.oip.major.system.model.UserModel;
import com.bjgoodwill.oip.major.system.service.UserService;
import com.bjgoodwill.oip.major.system.util.R; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; /**
* @Date 2018/7/13 10:50
* @Author HQueen
*/
@Controller
@RequestMapping(value="/user")
@Api(tags="用户信息")
public class UserController { @Autowired
UserService userService; /** 验证用户信息
* @param account
* @return
*/
@ApiOperation(value="验证用户信息", notes="验证用户信息")
@PostMapping(value="/verify")
@ApiImplicitParams({
@ApiImplicitParam(name="account", value="用户名", required=true, paramType="query", dataType="String")
})
@ResponseBody
public UserModel verifyUser(String account){ if (StringUtils.isEmpty(account)) {
throw new CoreException(ExceptionEnum.REQUEST_NULL);
} UserModel model = userService.findByAccount(account); return model;
} /** 跳转至用户修护界面
* @param id
* @param model
* @return
*/
@ApiOperation(value="跳转至用户修护界面", notes="跳转至用户修护界面")
@GetMapping(value="/pre/{id}")
@ApiImplicitParams({
@ApiImplicitParam(name="id", value="用户ID", required=true, paramType="path", dataType="Long")
})
public String preUpdate(Long id, Model model) { UserModel userModel = userService.getByPrimaryKey(id); model.addAttribute("model", userModel); return "edit";
} /** 修改用户基本信息
* @param userModel
* @return
*/
@ApiOperation(value="修改用户基本信息", notes="修改用户基本信息")
@PostMapping(value="/update")
@ResponseBody
public R update(@RequestBody @ApiParam(name="userModel", value="用户信息类") UserModel userModel) { if (userService.update(userModel) > 0) {
return R.ok();
} return R.error();
}
}

Spring Boot : Swagger 2的更多相关文章

  1. spring boot + swagger + mysql + maven

    1.首先编写 yaml 文件,创建项目所需的接口,在swagger.io官网上生成 spring boot项目: 2.由于生成的spring boot项目是公共类的所以还需要修改成所需的项目名称,主要 ...

  2. spring boot Swagger 集成

    1. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...

  3. Spring Boot + Swagger

    前言: 在互联网公司, 微服务的使用者一般分为两种, 客户端和其他后端项目(包括关联微服务),不管是那方对外提供文档 让别人理解接口 都是必不可少的.传统项目中一般使用wiki或者文档, 修改繁琐,调 ...

  4. Spring Boot --- Swagger基本使用

    1. pom <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <ar ...

  5. Spring Boot 集成Swagger

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

  6. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  7. HTTP RESTful服务开发 spring boot+Maven +Swagger

    这周配合第三方平台整合系统,需要提供HTTP REST服务和使用ActiveMQ推送消息,研究了下,做个笔记. 1.使用eclipse创建Spring Boot项目  创建Spring Boot项目( ...

  8. spring boot swagger-ui.html 404

    很奇怪的问题,找了好久. 因为spring boot+swagger实现起来很简单.看下面三部曲: 1.pom添加两个swagger依赖. <!-- Swagger依赖包 --> < ...

  9. Spring boot中使用springfox来生成Swagger Specification小结

    Rest接口对应Swagger Specification路径获取办法: 根据location的值获取api   json描述文件 也许有同学会问,为什么搞的这么麻烦,api json描述文件不就是h ...

随机推荐

  1. c++中的const关键字的理解

    看effective c++第二版推荐使用const,少用define.今天才发现发现这远远不够. #define定义的常量在预处理替换,debug的时候无法打印宏的,这种常量设置是有缺陷的, con ...

  2. 20.Ecshop 2.x/3.x SQL注入/任意代码执行漏洞

    Ecshop 2.x/3.x SQL注入/任意代码执行漏洞 影响版本: Ecshop 2.x Ecshop 3.x-3.6.0 漏洞分析: 该漏洞影响ECShop 2.x和3.x版本,是一个典型的“二 ...

  3. 工作随记--div最小高度

    给div添加最小高度 min-height:1000px;//IE7\FF height:100%;//IE6\IE7\FF 这个很重要,IE6定死高度后,需要再加上这条,才能自动延伸. _heigh ...

  4. nginx访问日志中添加接口返回值

    因为nginx作为web服务器时,会代理后端的一些接口,这时访问日志中只能记录访问接口的status码,也就是说,只能获得200.404 这些的值 那么如何获得接口返回的response值呢? 下面开 ...

  5. 【转】C#里partial关键字的作用

    源地址:http://www.cnblogs.com/OpenCoder/archive/2009/10/27/1590328.html

  6. 动手写一个简单版的谷歌TPU-矩阵乘法和卷积

    谷歌TPU是一个设计良好的矩阵计算加速单元,可以很好的加速神经网络的计算.本系列文章将利用公开的TPU V1相关资料,对其进行一定的简化.推测和修改,来实际编写一个简单版本的谷歌TPU.计划实现到行为 ...

  7. windows severs 2008r2 180天激活

    无需破解:Windows Server 2008 R2 至少免费使用 900天 1.首先安装后,有一个180天的试用期. 2.在180天试用期即将结束时,使用下面的评估序列号激活Svr 2008 R2 ...

  8. Docker 快速安装&搭建 Ngnix 环境,并配置反向代理

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  9. 转载 常用Jquery插件整理大全

    常用Jquery插件整理大全 做项目的时候总是少不了要用到Jquery插件,但是Jquery插件有太多,每次都要花费一些时间,因此本人就抽时间整理了一些Jquery插件,每个插件都有Demo或者是使用 ...

  10. web安全深度剖析pdf

    Web安全深度剖析.pdf_免费高速下载|百度网盘-分享无限制 链接:https://pan.baidu.com/s/1kVwP7SF