springboot配置swagger2
、在pom.xml里添加jar包:
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>${springfox.version}</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${springfox.version}</version>
- </dependency>
在pom.xml里的properties里添加版本
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <springfox.version>2.5.0</springfox.version>
- </properties>
2、在com/demo下创建swagger文件夹,创建SwaggerConfig文件
- package com.demo.swagger;
- 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;
- /**
- * Created by huguoju on 2016/12/29.
- */
- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
- /**
- * 可以定义多个组,比如本类中定义把test和demo区分开了 (访问页面就可以看到效果了)
- *
- */
- @Bean
- public Docket testApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.demo"))
- .paths(PathSelectors.any()).build();
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("Spring Boot中使用Swagger2构建RESTful APIs")
- .description("spring Boot 中构建RESTful API")
- .termsOfServiceUrl("")
- .contact("huguoju")
- .version("1.0")
- .build();
- }
- }
以上就完成了,在页面访问localhost:8080/swagger-ui.html,就看见了,下面主要说说怎么用
1、在com/dem/controller下创建TestController,
- package com.demo.controller;
- import com.demo.model.User;
- import com.demo.service.TestService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- /**
- * Created by huguoju on 2016/12/28.
- */
- @Controller
- @RequestMapping("test")
- @Api(value = "测试类",tags = "测试接口")
- public class TestController {
- @Autowired
- private TestService testService;
- @RequestMapping(value = "testData",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST,RequestMethod.GET})
- @ApiOperation("测试读写分离")
- public String testDateSource(
- @ApiParam(name = "userCode",value = "用户id",required = true)
- @RequestParam Integer userCode){
- User user=testService.selectByUserCode(userCode);
- Integer integer=testService.insertUser(user);
- return "oo";
- }
- }
以上就是controller里主要用到的@Api, @ApiOperation ,@ApiParam
在model里使用@ApiModelProperty("")
- import com.fasterxml.jackson.annotation.JsonIdentityInfo;
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
- import lombok.Data;
- import org.springframework.stereotype.Component;
- import java.util.Date;
- @Data
- public class User {
- @ApiModelProperty("用户id")
- private Integer userCode;
- @ApiModelProperty("用户类型")
- private String userType;
- @ApiModelProperty("用户名称")
- private String userName;
- @ApiModelProperty("用户手机号")
- private String mobileNumber;
- }
springboot配置swagger2的更多相关文章
- JAVA入门[23]-SpringBoot配置Swagger2
一.新建SpringBoot站点 1.新建module,然后引入pom依赖: <parent> <groupId>org.springframework.boot</gr ...
- SpringBoot配置swagger2(亲测有效,如果没有配置成功,欢迎在下方留言)
一.导包: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...
- SSM非springboot配置swagger2
前提:maven,ssm,不是springboot项目 1.在maven中添加依赖 <!-- Swagger2 Begin --> <!--springfox的核心jar包--> ...
- springboot 配置 swagger2
1.pom.xml 添加依赖 <!--swagger2 依赖--> <dependency> <groupId>io.springfox</groupId&g ...
- IDEA springboot配置
基于springboot2.1.7 springboot项目创建 springboot热部署 springboot配置swagger2 springboot配置mybatis springboot配置 ...
- springboot新增swagger2配置
转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...
- SpringBoot集成Swagger2并配置多个包路径扫描
1. 简介 随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...
- SpringBoot整合Swagger2,再也不用维护接口文档了!
前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...
- SpringBoot(七):SpringBoot整合Swagger2
原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...
随机推荐
- html+css3实现长方体效果
网上大都是正方体的效果,由于做一个东西需要,写了一个HTML+css3实现的长方体,有需要的也可以看看. 2017-07-25 21:30:23 h ...
- swift UIButton边框添加阴影效果
btn.layer.shadowOpacity = 0.8 //阴影区域透明度 btn.layer.shadowColor = UIColor.black.cgColor // 阴影区域颜色 btn. ...
- MySQL编程基础
本文是关于MySQL编程中的一些基础知识,包括变量和运算符.常用语句.函数. 一.变量与运算符 1.用户会话变量声明:SET @变量名 = 表达式;//即:用户会话变量无需提前定义,直接用赋值语句赋值 ...
- mysql的内连接,外连接(左外连接,右外连接)巩固
1:mysql的内连接: 内连接(inner join):显示左表以及右表符合连接条件的记录: select a.goods_id,a.goods_name,b.cate_name from tdb_ ...
- ASP.NET Core - 关于标签帮助器您值得了解的五点
如果您开发过ASP.NET Core Web应用程序,您应该已经熟悉了标签帮助器.ASP.NET Core应用程序依赖标签帮助器来呈现表单和表单字段是很常见的.所以,一个视图通常包含许多标签帮助器以及 ...
- shader之半兰伯特漫反射
看很多人实现shader都用插件shader force,那我还学shader干X!!!??? 好了,废话不多说,学习shader去.... 漫反射在shader里算是最基础的知识了.入手shader ...
- 怎样做才是最优雅方式切换 web 项目数据源 ?
随着业务变迁/需求变更,JavaEE 应用中会被迫连接多个数据源进行业务处理. 怎样在不影响原有项目结构的情况下,已最优雅/最简洁的方式动态切换数据源呢? 本文已一次添加数据源后动态切换实践为例,描述 ...
- Windows常用的一些DOS命令整理
Windows常用的一些DOS命令整理.. MS DOS 命令大全 ---清空Dos屏幕使用命令:cls -- Dos切换当前目录到D盘:cd d: 一.基础命令 1 dir 无参数:查看当前所在目录 ...
- 转每天一个linux命令(4):mkdir命令
linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...
- BestCoder Round #34_1002 以及 hdu 5191
枚举最终的W堆积木在哪,确定了区间,那么就需要把高于H的拿走,低于H的补上,高处的积木放到矮的上面,这样最优. 注意多出来的积木可以放在已有积木的前面或者后面,独立成一堆积木,所以需要在n堆积木的前后 ...