Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。

作用:

1. 接口的文档在线自动生成。

2. 功能测试。

1.添加pom.xml

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>

2.在启动类同级增加类

package com;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class Swagger2 {
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.taiji.demo.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("系统API接口") //创建人
.contact(new Contact("zhangyg", "www.redxun.com", "zhangyg@redxun.cn"))
//版本号
.version("1.0")
//描述
.description("系统API接口")
.build();
} }

3.编辑java 文件

package com.taiji.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController; import com.alibaba.fastjson.JSONObject;
import com.taiji.core.util.IdUtil;
import com.taiji.demo.model.SaleOrder;
import com.taiji.demo.service.SaleOrderService; 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;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses; @RestController
@Api(value="销售订单API接口")
public class SaleOrderController { @Autowired
private SaleOrderService saleOrderService; @ApiOperation(value="添加订单",notes="添加订单描述")
@RequestMapping(value="/add",method={RequestMethod.POST,RequestMethod.GET})
public void add(@ApiParam(value = "用户数据",
required = true,
type="JSONObject",
example="{\"name\":\"A\",\"userId\":\"B\",\"total\":\"100\"}") @RequestBody JSONObject json){
SaleOrder order=new SaleOrder();
order.setId(IdUtil.getId());
order.setName(json.getString("name"));
order.setCreator(json.getString("userId"));
order.setTotal(json.getDouble("total"));
saleOrderService.create(order);
} @ApiOperation(value="根据ID查看明细",notes="根据ID查看明细")
@ApiImplicitParams(value={
@ApiImplicitParam(name="id",value="订单ID",required=true,dataType="String")
})
@RequestMapping(value="/get/{id}",method=RequestMethod.GET)
public SaleOrder get(@PathVariable(value="id") String id){
SaleOrder order= saleOrderService.get(id);
return order;
} @RequestMapping(value="/page/{page}",method=RequestMethod.GET)
@ApiResponses({
@ApiResponse(response=JSONObject.class, code = 200, message = "订单列表")
})
public JSONObject page(@PathVariable(value="page") int page){
JSONObject order= saleOrderService.getAll(page,10);
return order;
}
}

4.访问平台列表

http://localhost:8000/demo/swagger-ui.html#/sale45order45controller

5.在线调试接口

输入好参数后,点击try it out 按钮,可以对代码进行调试。

springboot swagger 整合的更多相关文章

  1. SpringBoot+Swagger整合API

    SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...

  2. SpringBoot+Swagger整合

    0.引言及注意事项 Swagger是一个接口文档工具,依照Swagger可以0配置开发接口.不过要注意,Swagger是基于SpringBoot1.47版本开发的,而SpringBoot现在基本都是是 ...

  3. SpringBoot+Swagger2 整合

    SpringBoot+Swagger2四步整合 第一步:添加相关依赖 <parent> <groupId>org.springframework.boot</groupI ...

  4. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  5. SpringBoot与Swagger整合

    1 SpringBoot与Swagger整合https://blog.csdn.net/jamieblue1/article/details/99847744 2 Swagger详解(SpringBo ...

  6. springboot+swagger接口文档企业实践(上)

    目录 1.引言 2.swagger简介 2.1 swagger 介绍 2.2 springfox.swagger与springboot 3. 使用springboot+swagger构建接口文档 3. ...

  7. SpringBoot 同时整合thymeleaf html、vue html和jsp

    问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...

  8. SpringBoot+AOP整合

    SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...

  9. SpringBoot+Redis整合

    SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...

随机推荐

  1. Schtasks命令详解(计划任务DOS批处理)

    Schtasks 安排命令和程序定期运行或在指定时间内运行.从计划表中添加和删除任务,按需要启动和停止任务,显示和更改计划任务. 创建新的计划任务. 语法 schtasks/create/tnTask ...

  2. MySQL之Haproxy+Keepalived+MySQL高可用均衡负载部署 (网络摘抄)

    来源于:https://blog.csdn.net/weisong530624687/article/details/71536837?utm_source=blogxgwz3 一.安装主从MySQL ...

  3. webservice的简单使用,cxf框架的的使用

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...

  4. (整理)EF分页的实现

    最近做一个小功能,需要数据分页,因为小框架使用的是EF,因此查询了一下EF的分页. EF分页主要用到了skip和take两个方法: GetListBy(lamda xxxxx).skip(PageSi ...

  5. 同步对象(同步条件Event)

    event = threading.Event()   #创建同步对象 event.wait()     #同步对象等待状态 event.set() #同步对象设置Trueevent.clear()  ...

  6. Spring 4 官方文档学习 Web MVC 框架

    1.介绍Spring Web MVC 框架 Spring Web MVC 框架是围绕DispatcherServlet设计的,所谓DispatcherServlet就是将请求分发到handler,需要 ...

  7. uname命令详解

    1.简介: uname命令用于打印当前系统相关信息(内核版本号.硬件架构.主机名称和操作系统类型等). 2.命令语法: uname (选项) 3.选项: -a或--all:显示全部的信息: -m或-- ...

  8. 吴裕雄 python深度学习与实践(14)

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt threshold = 1.0e-2 x1_dat ...

  9. C#//字节数组转16进制字符串

    //字节数组转16进制字符串 private static string byteToHexStr(byte[] bytes,int length) { string returnStr = &quo ...

  10. nginx实现http www服务的方式