1、新建springboot项目,可以通过https://start.spring.io/快速生成springboot项目。

2、引入jar依赖:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>

3、编写Controller,并配置相应的注解,见下图

package com.demo.incubator.swaggerdemo.controller;

import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
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; @RestController
@RequestMapping("/demo")
@Api(protocols = "http,https", produces = "application/json", tags = "mock样例")
public class SwaggerDemoController {
@PostMapping("/mokeInvoke")
@ApiOperation(value = "mock方法样例", notes = "返回一个字符串")
public String mockInvoke(@ApiParam(value = "显示的信息", required = true) @RequestParam("showMsg") String showMsg) {
return showMsg;
} @PostMapping("/mokeInvoke2")
@ApiOperation(value = "mock方法样例2", notes = "返回一个字符串")
@ApiImplicitParams({@ApiImplicitParam(value = "手机号", name = "userPhone", required = true, paramType = "query"),
@ApiImplicitParam(value = "姓名", name = "userName", required = true, paramType = "query"),
@ApiImplicitParam(value = "地址", name = "address", required = true, paramType = "query"),
@ApiImplicitParam(value = "系统来源", name = "appid", required = true, paramType = "header")})
public String mockInvoke2(@RequestBody Map<String, Object> params) {
return JSON.toJSONString(params);
} }

4、启动项目,访问链接地址http://localhost:8080/swagger-ui/index.html(端口换成自己配置的端口),即可看到swagger页面,如下图

5、但是会发现多出来basic-error-controller、operation-handler、web-mvc-links-handler,可以通过新建一个Configration来消除,只保留我们对外提供的接口

package com.demo.incubator.swaggerdemo.config;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; @Configuration
public class SwaggerConfiguration {
//注意标黄的地方要求能匹配到你所有的接口路径,比如我的例子中两个接口都是以demo开始的,/demo/mockInvoke和/demo/mockInvoke2
@Bean
Docket xiaokeai() {
return new Docket(DocumentationType.OAS_30).useDefaultResponseMessages(false)
.produces(Stream.of("application/xml", "application/json").collect(Collectors.toSet())).select()
.paths(PathSelectors.regex("/demo/.*")).build()
.protocols(Stream.of("http", "https").collect(Collectors.toSet()));
}
}

6、再次启动项目,就会发现只保留了自有的controller暴露的接口。

swagger3.0(springboot)消除basic-error-controller的更多相关文章

  1. SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂)

    SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂) 一,整合Swagger3.0 随着Spring Boot.Spring Cloud等微服务的 ...

  2. springboot - 返回xml error 从自定义的 ErrorController

    1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...

  3. [更新]一份包含: 采用RSA JWT(Json Web Token, RSA加密)的OAUTH2.0,HTTP BASIC,本地数据库验证,Windows域验证,单点登录的Spring Security配置文件

    没有任何注释,表怪我(¬_¬) 更新: 2016.05.29: 将AuthorizationServer和ResourceServer分开配置 2016.05.29: Token获取采用Http Ba ...

  4. wince6.0 编译报错:"error C2220: warning treated as error - no 'object' file generated"的解决办法

    内容提要:wince6.0编译报错:"error C2220: warning treated as error - no 'object' file generated" 原因是 ...

  5. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  6. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍

    原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...

  7. Prelogin error: host 127.0.0.1 port 1434 Error reading prelogin response: Connection reset ClientConnectionId:26d4b559-c985-4b2e-bd8e-dd7a53b67e48

    我在使用SSM框架的时候,连接的是sqlserver 2008r2数据库,但是查询数据的时候总是出现这样的警告信息,导致的结果是第一次登录的时候获取数据慢或者获取数据失败,具体的log信息如下 警告: ...

  8. LoadLibrary(C:\soft\IDA 7.0\IDA 7.0\plugins\python64.dll) error: 找不到指定的模块。 C:\soft\IDA 7.0\IDA 7.0\plugins\python64.dll: can't load file LoadLibrary(C:\soft\IDA 7.0\IDA 7.0\plugins\python64.dll) erro

    LoadLibrary(C:\soft\IDA 7.0\IDA 7.0\plugins\python64.dll) error: 找不到指定的模块. C:\soft\IDA 7.0\IDA 7.0\p ...

  9. Springboot消除switch-case方法

    Springboot消除switch-case方法 背景 最近,在使用springboot开发一个接口的时候,需要根据接收的请求事件类型,去执行不同的操作,返回不同的结果,基本逻辑如下: String ...

  10. ERROR [RMI TCP Connection(3)-127.0.0.1] - init datasource error

    运行报错 ERROR [RMI TCP Connection(3)-127.0.0.1] - init datasource error, url: jdbc:mysql://localhost:33 ...

随机推荐

  1. Python调用函数带括号和不带括号的区别

    1.不带括号时,调用的是这个函数本身 ,是整个函数体,是一个函数对象,不需等该函数执行完成 2.带括号(此时必须传入需要的参数),调用的是函数的return结果,需要等待函数执行完成的结果 如果函数本 ...

  2. TP框架 商城前台用户注册方法

    //注册 public function register(){ if(IS_POST){ //接收数据 $data = I('post.'); $model = D('User'); $data[' ...

  3. Spring Cloud Gateway 没有链路信息,我 TM 人傻了(下)

    本系列是 我TM人傻了 系列第五期[捂脸],往期精彩回顾: 升级到Spring 5.3.x之后,GC次数急剧增加,我TM人傻了 这个大表走索引字段查询的 SQL 怎么就成全扫描了,我TM人傻了 获取异 ...

  4. DeDeCMS v5.7 漏洞复现

    DedeCMS V5.7 漏洞复现 XSS漏洞 首先我们在首页要进行用户的注册以及登录 这里我们已经提前注册过了,登录即可 普通用户账号密码:root/passwd 管理员账号密码:admin/pik ...

  5. P3343-[ZJOI2015]地震后的幻想乡【dp,数学期望】

    正题 题目链接:https://www.luogu.com.cn/problem/P3343 题目大意 给出\(n\)个点的一张无向图,每条边被修复的时间是\([0,1]\)的一个随机实数,求这张图联 ...

  6. WPF进阶技巧和实战03-控件(5-列表、树、网格04)

    ListView控件 ListView继承自简单的没有特色的ListBox,增加了对基于列显示的支持,并增加了快速切换视图或显示模式的能力,而不需要重新绑定数据以重新构建列表. ListView类继承 ...

  7. Javascript设计模式之原型模式、发布订阅模式

    原型模式 原型模式用于在创建对象时,通过共享某个对象原型的属性和方法,从而达到提高性能.降低内存占用.代码复用的效果. 示例一 function Person(name) { this.name = ...

  8. Java(2)详解注释&关键字&常量&变量&标识符

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201497.html 博客主页:https://www.cnblogs.com/testero ...

  9. PG集群(PostgreSql环境)搭建

    centos PG集群搭建 一.安装PG 1.安装之前首先查看软件是否已经安装 rpm -qa | grep postgresql #若存在,需要卸载使用 yum remove postgresql ...

  10. 力扣 - 剑指 Offer 45. 把数组排成最小的数

    题目 剑指 Offer 45. 把数组排成最小的数 思路1 将整数数组转化成字符串数组 然后使用Arrays工具类的sort方法帮助我们排序 代码 class Solution { public St ...