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. phpwind v9存在命令执行漏洞(登陆后台)

    已知漏洞:https://www.seebug.org/vuldb/ssvid-94465 phpwind v9最新版存在命令执行漏洞(登陆后台) Phpwind_v9.0.2(最新版),phpwin ...

  2. TypeScript 模块系统

    https://www.cnblogs.com/niklai/p/5808789.html

  3. 用PS做圆角图片

    ps: Adobe Photoshop CS2  如果图片被锁定,请“双击”图层中“背景”解锁,如果没有图层菜单,在最上面导航栏中:窗口—图层.如下图:  点“确定”,解锁.  选用“圆角矩形工具”. ...

  4. QQ第三方登录(预备节)

    第一次很多时候,用户因为复杂的注册程序,而不愿意再体验我们的网站服务,我们可以通过QQ第三方授权,这样既保证了用户数,而且也极大的提高了用户体验.废话就不多说了,直接进入正题... 要实现QQ第三方登 ...

  5. 搭建基于MySQL的读写分离工具Amoeba

    搭建基于MySQL的读写分离工具Amoeba: Amoeba工具是实现MySQL数据库读写分离的一个工具,前提是基于MySQL主从复制来实现的: 实验环境(虚拟机): 主机 角色 10.10.10.2 ...

  6. linux如何复制文件夹和移动文件夹

    linux下文件的复制.移动与删除命令为:cp,mv,rm一.文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination)cp [option ...

  7. awk选取制定行数,条件判断等

    awk '{if(NR%5==0){print}}' your_file 取出可以被5整除的数awk '{if(NR<=300){print}}' your_file 取出行数小于300的数据a ...

  8. SSH登录启用Google二次身份验证

    一般来说,使用ssh远程登录服务器,只需要输入账号和密码,显然这种方式不是很安全.为了安全着想,可以使用GoogleAuthenticator(谷歌身份验证器),以便在账号和密码之间再增加一个验证码, ...

  9. fastJson注解@JSONField使用的一个实例

    1.实体类 package jsonArrayjsonObject.cn; import java.io.Serializable; import java.util.Date; import com ...

  10. unable to auto-detect email address

    git错误:unable to auto-detect email address 2017年11月14日 08:51:08 陈君豪 阅读数:7914   idea 用git更新的时候报错,详细错误信 ...