spring boot集成swagger2:
    swagger2是一个基于restful的开源设计,构建,文档,访问的开源工具集.开发中它的在线可视化文档功能,可以动态生成文档,简化前后对接工作.以下是Java在spring boot中使用方式:    
  • 引入maven依赖:

  springfox-swagger2是swagger核心代码;springfox-swagger-ui提供静态jsUI可视化页面

        <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
 
  • 启用swagger
package com.ssth.exchange.exsite;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication(scanBasePackages = { "com.ssth.exchange.exsite"})
@Configuration
@MapperScan("com.ssth.exchange.exsite.mapper")
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

使用@EnableSwagger2注解启用swagger

  • 在接口中添加对应注解
package com.ssth.exchange.exsite.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.ssth.exchange.exsite.controller.basic.ExsiteBaseController;
import com.ssth.exchange.exsite.controller.request.NavigationReq;
import com.ssth.exchange.exsite.controller.response.NavigationResp;
import com.ssth.exchange.exsite.controller.response.ResponseEntity;
import com.ssth.exchange.exsite.service.HomeService; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; /**
* @Description 首页控制器
* @author chengmuyu
* @date 2018年5月31日 下午2:31:12
*/
@RestController
@RequestMapping("/home")
@ResponseBody
@CrossOrigin
@Api(value="首页接口",consumes="application/json")
public class HomeController extends ExsiteBaseController { @Autowired
private HomeService homeService; /**
* @Description 获取导航列表
* @param nav 导航查询参数
* @return
*/
@RequestMapping(value = "list/nav",method = RequestMethod.POST)
@ApiOperation(value="获取导航列表", httpMethod = "POST", response = NavigationResp.class)
public ResponseEntity<List<NavigationResp>> listNavigation(@RequestBody NavigationReq nav) {
return ResponseEntity.successResponse(homeService.listNavigation(nav.getPid()));
}
}
对应注解说明:
@Api:修饰整个类,value:描述Controller的作用,consumes:声明参数类型
@ApiOperation:修饰请求接口.value:描述接口;httpMethod:接口请求方式;response:定义接口响应实体;
 
 
  • 实体声明定义
package com.ssth.exchange.exsite.controller.request;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; /**
* @Description 导航请求实体
* @author chengmuyu
* @date 2018年5月31日 下午3:04:05
*/
@ApiModel(value = "导航请求")
public class NavigationReq {
@ApiModelProperty(value = "导航父id,null表示查询一级目录")
private String pid;
}
@ApiModel:修饰实体类
@ApiModelProperty:修饰实体参数
 
 
  • Swagger常用注解
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数

  

 
官方示例文档地址:
 

spring boot + swagger2的更多相关文章

  1. Java | Spring Boot Swagger2 集成REST ful API 生成接口文档

      Spring Boot Swagger2 集成REST ful API 生成接口文档 原文 简介 由于Spring Boot 的特性,用来开发 REST ful 变得非常容易,并且结合 Swagg ...

  2. spring boot☞Swagger2文档构建及单元测试

    首先,回顾并详细说明一下在快速入门中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  3. (转) 增加 header 参数,spring boot + swagger2(springfox)

    1 @Configuration 2 @EnableSwagger2 3 public class Swagger2 { 4 @Bean 5 public Docket createRestApi() ...

  4. Spring Boot Swagger2自动生成接口文档

    一.简介 在当下这个前后端分离的技术趋势下,前端工程师过度依赖后端工程师的接口和数据,给开发带来了两大问题: 1.问题一.后端接口查看难:要怎么调用?参数怎么传递?有几个参数?参数都代表什么含义? 2 ...

  5. spring boot Swagger2(version=2.7.0) 注解@ApiImplicitParam的属性dataType值为”自定义泛型“应用

    注解: @ApiImplicitParams @ApiImplicitParam    name="需注解的API输入参数", value="接收参数的意义描述" ...

  6. Spring Boot(九)Swagger2自动生成接口文档和Mock模拟数据

    一.简介 在当下这个前后端分离的技术趋势下,前端工程师过度依赖后端工程师的接口和数据,给开发带来了两大问题: 问题一.后端接口查看难:要怎么调用?参数怎么传递?有几个参数?参数都代表什么含义? 问题二 ...

  7. spring boot 2.x 系列——spring-boot 集成 Swagger2 打造在线接口文档

    文章目录 一.Springfox 与 Swagger 简介 1.1 Springfox 1.2 Swagger 1.3 OpenApi.Swagger.Springfox的关系 二.spring bo ...

  8. Spring Boot 系列教程9-swagger-前后端分离后的标准

    前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...

  9. 国内最全的Spring Boot系列之二

    历史文章 <国内最全的Spring Boot系列之一> 视频&交流平台 SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http:/ ...

随机推荐

  1. svn之合并分支

    学习连接 svn的merge使用例子

  2. tfs如何为工作项添加变更集

    今天工作中遇到的,可惜之前没怎么用过TFS. 我这是最后一次签入的时候关联了工作项.目的是要把先前签入的绑定到该任务上. 团队自愿管理器->查找历史记录->双击最后一次绑定工作项的变更集- ...

  3. mongoose整理笔记

    一:参考学习网址 npm: https://www.npmjs.com/package/mongoose 官网API:http://mongoosejs.com/docs/guide.html 二:在 ...

  4. js入门之函数

    一. 函数 函数可以封装一段特定功能的代码,然后通过函数名可以重复调用 1 .函数的定义 funcation 函数名 (){ 函数体 } 函数名() 调用函数 2. 函数的参数 funcation f ...

  5. docker-社区版(CE)安装

    目录 docker-社区版(CE)安装 安装步骤 docker-社区版(CE)安装 该安装方法是 基于centeros7 及其以上版本的安装方式,完全参考 docker官网提供的安装文档,官网安装文档 ...

  6. xposed获取类的属性成员

    XposedBridge.log("开始获取属性:"); Field[] fields = param.thisObject.getClass().getDeclaredField ...

  7. 【Struts2】Json插件使用

    一.使用步骤 1.1 引入依赖 1.2 在struts.xml文件中配置 一.使用步骤 1.1 引入依赖 <!-- https://mvnrepository.com/artifact/org. ...

  8. 企业级自动化运维工具应用实战ansible

    公司计划在年底做一次大型市场促销活动,全面冲刺下交易额,为明年的上市做准备.公司要求各业务组对年底大促做准备,运维部要求所有业务容量进行三倍的扩容,并搭建出多套环境可以共开发和测试人员做测试,运维老大 ...

  9. cmd生成大文件

    用cmd生成一个大小一定的文件 输入fsutil file createnew  文件位置  文件大小(以字节为单位1024b=1kb) 列如:fsutil file createnew  d:\my ...

  10. zend studio 13.6.1中文乱码的解决办法

    进入窗口--->首选项--->常规--->工作空间 window=>preference=>General=>workspace 照图设置后重启软件即可