配置类

package top.yalong;

import org.springframework.beans.factory.annotation.Value;
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; /**
* Swagger2 配置
*
* @author liuyalong
*/
@Configuration
@EnableSwagger2
public class Swagger2 { @Value("${swagger.show}")
private boolean swaggerShow; private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Lyl's API")
// 开发人员信息
//.contact(new Contact("Yalong Liu","http://www.yalong.top","4379711@qq.com"))
.version("1.0")
//.description("刘亚龙的接口文档")
.build();
} @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(swaggerShow)
.apiInfo(apiInfo())
.groupName("用户接口")
.select()
// 可以扫描指定的某个包
// .apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.basePackage("top.yalong"))
.paths(PathSelectors.any())
.build(); }
}

pom

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

yaml设置属性是否开启

swagger:
show: true

controller使用示例

package top.yalong.web.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yalong.pojo.user.User;
import top.yalong.service.user.UserService;
import top.yalong.common.error.MyException; import java.util.List; /**
* @author liuyalong
*/
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; @ApiOperation(value = "查询用户", notes = "根据id来查询用户")
@GetMapping("/query/{id}")
public List<User> query(@PathVariable int id) {
if (id == 0) {
throw new RuntimeException("我自己抛出一个全局异常");
} else if (id == 1) {
throw new MyException(9523, "我自己抛出一个异常");
} return userService.query();
} }

Swagger2配置的更多相关文章

  1. Swagger2配置与使用

    Swagger2配置与使用 Swagger2介绍 前后端分离开发模式中,api文档是最好的沟通方式. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 We ...

  2. springboot新增swagger2配置

    转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...

  3. swagger2配置和使用

    1.导入swagger2 <dependency> <groupId>io.springfox</groupId> <artifactId>spring ...

  4. 使用swagger2配置springboot时出现的问题

    这个问题踩了几次坑了,这次又遇到了,不记录一下看来是不长记性了: 测试普通的增删改查的时候,发现删除和查询是对的,可是增加和更新却数据绑定不到controller的参数上面去. 因为是自定义的实体类, ...

  5. Swagger2 配置

    1. 每个请求都需要换取key: @Bean public Docket createRestApi() { //添加head参数start ParameterBuilder appId = new ...

  6. swagger2配置详解

    1.写在controller上的注解 1.1 @Api 代码 @Api(tags = "用户相关接口", description = "提供用户相关的 Rest API& ...

  7. spring clould -多模块 -swagger2 配置 nginx 的正确设置

    #user nobody; worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  8. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  9. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

随机推荐

  1. showengineinnodbstatus的解读

    如何查看innodb的相关信息 ---------------------- BUFFER POOL AND MEMORY ---------------------- Total memory al ...

  2. Jrebel & Xrebel 在线激活方法 (亲测可用)

    一开始用eclipse的时候虽然这是一个狂吃内存的家伙,但是调试代码是真的舒服,修改过的代码可以不用重启热加载,后来转idea,虽然idea很完美但是也有不足的地方,比如代码调试就不能热加载. 还好有 ...

  3. Python 调用Get接口

    import requests,jsonurl = 'http://localhost:30627/api/jobs/GetNuberId?id=2'req = requests.get(url)re ...

  4. linux系统中 SElinux安全子系统

    1.SElinux 是什么? SElinux(Security-Enhanced Linux)是美国国家安全局在linux开源社区的帮助下开发的一个强制访问控制(Mandatory Access Co ...

  5. Springboot 框架整理,建议做开发的都看看,整理的比较详细!

    什么是 Spring Boot? SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品,是用来简化 spring 初始搭建和开 ...

  6. SpringBoot整合MyBatis,HiKari、Druid连接池的使用

    SpringBoot整合MyBatis 1.创建项目时勾选mybatis.数据库驱动.   mysql驱动默认是8.x的版本,如果要使用5.x的版本,创建后到pom.xml中改. 也可以手动添加依赖 ...

  7. LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating

    LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating [问题描述] The cross-country skiing course at the winter Mool ...

  8. python办公入门4:xlrd操作excel行

    操作excel行 1 #通过索引获取操作行 2 sheet=data.sheet_by_index(0) 3 #获取当前sheet下的有效行数 4 print(sheet.nrows) 5 #获取某一 ...

  9. 【模板】【P3402】可持久化并查集

    (题面来自洛谷) 题目描述 n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 ...

  10. JavaSE 学习笔记04丨异常

    Chapter 9 异常 异常:指程序在执行过程中,出现的非正常的情况,最终导致JVM非正常停止. 在Java等面向对象的编程语言中,异常是一个类,所有异常都是发生在运行阶段的(因为也只有程序运行阶段 ...