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文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...
随机推荐
- cvim常用快捷键
esc:返回浏览模式 h.j.k.l:上.下.左.右 u/e.d:上半屏.下半屏 H.L:后退.前进 f.F:直接打开.后台打开 gg.G:页头.页尾 gi.gI:第一个输入框.最后一个输入框 q ...
- HTML 基本标签02
02-html基本标签 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- 对图像组成不了解?这样学习Matplotlib必走弯路!
在学习Matplotlib的过程中,大家一定会遇到这样那样的问题,比如说,背景图怎么设置?坐标轴怎么设置?坐标轴上的刻度值怎么设置?怎样在PyQt中添加Matplotlib绘图模块? 其实想要学好用好 ...
- 喂,前端,你应该知道的chrome插件
最近,优点闲. 压力,有点大,回顾,曾今被问,你怎么查看内存泄露,然后,一脸蒙. 工欲善其事, 必先利其器 最近在研究chrome devtools,发现,其实他很强.而且chrome6周一次的更新, ...
- Ubuntu16.04LTS 环境下编译安装Xen
一.编译安装xen4.6过程 操作系统使用ubuntu16.04,通过下载xen4.6的源代码并编译安装来创建xen4.6环境. 一.依赖包的安装 sudo apt-get install gcc m ...
- 一步一步学多线程-Timer
在执行定时任务的时候可以用Timer来实现,现在小编对学到的进行一次总结,先来看一个demo 1 public class TimerTest { 2 3 public static void mai ...
- python学习之元组与文件
元组 元组是最后一个python集合类型.元组由简单的对象构成,元组与列表非常相似,只不过元组不能在原处修改,并且通常写成圆括号,虽然元组部支持任何方法调用,但元组具有列表的大多数属性. 实际应用中的 ...
- 熟悉JS中的常用选择器及属性、方法的调用
选择器.属性及方法调用的配合使用: <style> #a{ width: 200px; height: 1 ...
- UML类图几种关系的总结(网摘)
Source link:http://www.open-open.com/lib/view/open1328059700311.html UML类图几种关系的总结 原文出处:[深度开源] 在UML类图 ...
- ReactiveCocoa应用篇(一)
作为函数式编程和响应式编程的代表ReactiveCocoa,自一出世便受到广大码农的热爱,结合MVVM编程模式,不仅能够方便的处理各种交互事件.降低模块间的耦合性,还能简化项目结构,降低阅读和维护的成 ...