配置类

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. ListView的HeaderView包含的GridView滑动隐藏后无法点击问题分析

    目录 1 现象 2 问题分析 2.1 滑动前 2.2 滑动后 2.3 mDataChanged赋值为true的位置 2.3 GridView直接作为ListView的HeaderView为什么可以滑动 ...

  2. MyBatis 使用手册

    MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作.MyBatis 可以通过简单的 XM ...

  3. rinetd小工具

    一.介绍 tcp端口转发工具,针对ip:port的 连接进行转发.重点开源,且配置简单.不支持ftp,因为ftp要使用多个端口. 1.转发规则 [root@master tmp]# cat a.con ...

  4. Java中常见内存溢出模拟及错误分析

    在JVM虚拟机规范中,Java虚拟机运行时数据区域除了程序计数器(Program Counter Register)外都有可能出现OutOfMemoryError的情况,使用Hotspot虚拟机简单的 ...

  5. PHP+Ajax点击加载更多内容

    css样式: <style type="text/css"> #more{margin:10px auto;width: 560px; border: 1px soli ...

  6. jboss反序列化漏洞实战渗透笔记

    一.利用shodan,fofa或谷歌搜索关键字:8080/jmx-console/ 二.下载java反序列化终极测试工具进行验证漏洞 三.记住User Current Directory:  C:\j ...

  7. java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊

    Spring 1.1.1.1 创建一个bean package com.zt.spring; public class MyBean { private String userName; privat ...

  8. Mac读写ntfs软件究竟哪一款适合我们?

    生活中我们免不了会使用一些硬盘设备来存储文件或者是数据,然而绝大多数的移动硬盘都是ntfs格式.Mac读写ntfs软件有很多,究竟哪一款适合我们? 首先,我们一起了解一下什么是ntfs格式.ntfs, ...

  9. React Native两种加载图片的方式

    1 加载网络图片 通过uri就可以加载网络图片 <Image source={{uri:'http://facebook.github.io/react/img/logo_og.png'}} s ...

  10. selenium元素定位检查

    自动化测试的重点就是定位元素,然而定位元素没办法一步一步运行调试检查,每次都需要从代码的开始运行,检查起来效率极低. 一直想找一个能检查唯一性工具或者方法.百度发现一篇文章https://blog.c ...