swagger3.0(springboot)消除basic-error-controller
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的更多相关文章
- SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂)
SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂) 一,整合Swagger3.0 随着Spring Boot.Spring Cloud等微服务的 ...
- springboot - 返回xml error 从自定义的 ErrorController
1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...
- [更新]一份包含: 采用RSA JWT(Json Web Token, RSA加密)的OAUTH2.0,HTTP BASIC,本地数据库验证,Windows域验证,单点登录的Spring Security配置文件
没有任何注释,表怪我(¬_¬) 更新: 2016.05.29: 将AuthorizationServer和ResourceServer分开配置 2016.05.29: Token获取采用Http Ba ...
- wince6.0 编译报错:"error C2220: warning treated as error - no 'object' file generated"的解决办法
内容提要:wince6.0编译报错:"error C2220: warning treated as error - no 'object' file generated" 原因是 ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别
SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...
- 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信息如下 警告: ...
- 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 ...
- Springboot消除switch-case方法
Springboot消除switch-case方法 背景 最近,在使用springboot开发一个接口的时候,需要根据接收的请求事件类型,去执行不同的操作,返回不同的结果,基本逻辑如下: String ...
- 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 ...
随机推荐
- mybatis一对多联表查询的两种常见方式
1.嵌套结果查询(部分代码如下) sql语句接上: 注释:class表(c别名),student表teacher(t别名)teacher_id为class表的字段t_id为teacher表的字段,因为 ...
- 用Python做了个奇奇怪怪的打篮球游戏
一.前言 准备编写一个篮球游戏,运动员带球跑,跳起投篮.在每帧图片中包括运动员和篮球,使用多帧图片,实现运动员运球跑动的效果. 运动员运球跑动作每帧图形的宽和高可能不同,例如,跨一大步,和两腿并拢,其 ...
- Jmeter系列(20)- 录制控制器
作用:相当于对录制的脚本进行分组存放,放在同一个线程组里面:录制脚本的时候,选择线程组下面想要存放的录制控制器中
- js运行机制 值引用 值传递
1.js是单线程的 为什么是单线程的呢 因为js作为浏览器脚本语言,会有很多和用户的互动,以及操作dom,多个线程会出问题. 2.js有同步任务,异步任务(ajax,用户点击等,settimeou ...
- whistle浏览器抓包(以火狐浏览器为例)
环境:whistle:1.14.6 whistle浏览器抓包 以火狐浏览器为例 1.启动whistle 使用w2 start启动whistle. 退出cmd后,whistle自动关闭了,所以每次使用w ...
- 『GoLang』字典Map
map是一种元素对的无序集合,一组称为元素value,另一组为唯一键索引key. 未初始化map的值为nil.map 是引用类型,可以使用如下声明: var map1 map[keytype]valu ...
- Kettle学习笔记(二)— 基本操作
目录 Kettle学习笔记(一)- 环境部署及运行 Kettle学习笔记(二)- 基本操作 kettle学习笔记(三)- 定时任务的脚本执行 Kettle学习笔记(四)- 总结 打开Kettle 打开 ...
- git批量处理git author和commit
最近在做自己项目的时候,由于使用了git全局配置的用户名和邮箱,导致自己私人的仓库里面的所有提交记录都是用的公司的邮箱和用户名,于是想批量替换一下. 可以在需要修改的项目的根目录下使用如下命令,进行批 ...
- 这两个基础seo插件,wordpress网站必装
WordPress对搜索引擎非常友好,这一点很多人都知道.不过我们在制作完成WordPress主题后,还可以在原来的良好基础上,添加两个队seo非常有利的WordPress插件. 第一个插件:Baid ...
- nginx配置禁止爬虫配置
1.在配置文件里添加禁止爬虫配置 server { ------ #添加如下内容即可防止爬虫 if ($http_user_agent ~* "qihoobot|Baiduspider|Go ...