首页 > 其他分享 >Spring boot整合 Swagger2 以及遇到的坑

Spring boot整合 Swagger2 以及遇到的坑

时间:2022-12-19 11:35:02浏览次数:68  
标签:Spring boot value 参数 Swagger2 注解 import springfox id


一、引入依赖:

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

注意了注意了知识点! 

版本一定要2.5.0以及以上版本!

2.2.2版本的与feign有冲突! 会报bean创建加载异常! 

-----------------------------------------------------------------------------------------------

二、Swagger2配置文件类:

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;

/**
* @ClassName: swagger2配置
* @Description: TODO
* @author 刘圈圈
* @date 2018年7月5日
*/

@Configuration
@EnableSwagger2
public class Swagger2 {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("io.sr.modules.tra.app"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("旅游用车 APIs")
.description("--------------------------------")

.contact("刘圈圈")
.version("0.1.1")
.build();
}

}

这个类要和启动类放在同一个目录下, 

用@Configuration注解该类,等价于XML中配置beans;用@Bean标注方法等价于XML中配置bean。

Application.class 加上注解@EnableSwagger2 表示开启Swagger,也可以在配置文件上加@EnableSwagger2

-----------------------------------------------------------------------------------------------

我的包结构:

Spring boot整合 Swagger2 以及遇到的坑_ci

.apis(RequestHandlerSelectors.basePackage("io.sr.modules.tra.app"))  : 是设置扫描的包路径

io.sr.modules.tra.app 它是模糊匹配的,所以我们在创建包还有URL时要避免这种格式

result接口:

这一步不是必要的, 只要swagger扫描到@RestController注解的类就会按默认配置生成接口文档

三、controller代码:

@RestController
@RequestMapping("/user")
@Api(value = "Shop")
public class SpringBootController {

@ApiOperation(value = "获取helloWorld", notes = "简单SpringMVC请求")
@RequestMapping("/")
String home() {
return "HELLO WORLD";
}

@ApiOperation(value = "获得A+B", notes = "根据url的classNo和url的studentName获得请求参数的字符串相加,RestFul风格的请求")
@ApiImplicitParams({@ApiImplicitParam(name = "classNo", value = "班级编号", required = true, dataType = "String"),
})
@RequestMapping(value = "/class/{classNo}/to/{studentName}", method = RequestMethod.GET)
String world(@PathVariable("classNo") String classNo, @PathVariable("studentName") String studentName) {
return classNo + " " + studentName;
}


}

访问:http://localhost:8080/swagger-ui.html#/:

Spring boot整合 Swagger2 以及遇到的坑_JSON_02

四、Swagger注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

 Controller注解

  • @Api:修饰整个类,描述Controller的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiImplicitParam:一个请求参数
  • @ApiImplicitParams:多个请求参数
  • @ApiProperty:用对象接收参数时,描述对象的一个字段
  • @ApiResponse:HTTP响应其中1个描述
  • @ApiResponses:HTTP响应整体描述
  • @ApiIgnore:使用该注解忽略这个API
  • @ApiError :发生错误返回的信息

JavaBean注解

@ApiModel:用对象来接收参数(Javabean 类上的注解)

@ApiModelProperty 作用在javabean的属性上(基本数据类型,自定义数据类型都可)

作用范围

API

使用位置

对象属性

@ApiModelProperty

用在出入参数对象的字段上

协议集描述

@Api

用于controller类上

协议描述

@ApiOperation

用在controller的方法上

Response集

@ApiResponses

用在controller的方法上

Response

@ApiResponse

用在 @ApiResponses里边

非对象参数集

@ApiImplicitParams

用在controller的方法上

非对象参数描述

@ApiImplicitParam

用在@ApiImplicitParams的方法里边

描述返回对象的意义

@ApiModel

用在返回对象类上

@RequestMapping此注解的推荐配置 
value 
method 
produces

示例:

@ApiOperation("信息软删除")
@ApiResponses({ @ApiResponse(code = CommonStatus.OK, message = "操作成功"),
@ApiResponse(code = CommonStatus.EXCEPTION, message = "服务器内部异常"),
@ApiResponse(code = CommonStatus.FORBIDDEN, message = "权限不足") })
@ApiImplicitParams({ @ApiImplicitParam(paramType = "query", dataType = "Long", name = "id", value = "信息id", required = true) })
@RequestMapping(value = "/remove.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public RestfulProtocol remove(Long id) {
@ApiModelProperty(value = "标题")
private String title;

@ApiImplicitParam

属性

取值

作用

paramType

查询参数类型

path

以地址的形式提交数据

query

直接跟参数完成自动映射赋值

body

以流的形式提交 仅支持POST

header

参数在request headers 里边提交

form

以form表单的形式提交 仅支持POST

dataType

参数的数据类型 只作为标志说明,并没有实际验证

Long

String

name

接收参数名

value

接收参数的意义描述

required

参数是否必填

true

必填

false

非必填

defaultValue

默认值

paramType 示例详解

path

@RequestMapping(value = "/findById1/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

@PathVariable(name = "id") Long id

body

@ApiImplicitParams({ @ApiImplicitParam(paramType = "body", dataType = "MessageParam", name = "param", value = "信息参数", required = true) })
@RequestMapping(value = "/findById3", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)

@RequestBody MessageParam param

提交的参数是这个对象的一个json,然后会自动解析到对应的字段上去,也可以通过流的形式接收当前的请求数据,但是这个和上面的接收方式仅能使用一个(用@RequestBody之后流就会关闭了)

header

@ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "Long", name = "id", value = "信息id", required = true) }) 

String idstr = request.getHeader("id");
if (StringUtils.isNumeric(idstr)) {
id = Long.parseLong(idstr);
}

Form

@ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType = "Long", name = "id", value = "信息id", required = true) })
@RequestMapping(value = "/findById5", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)


代码: ​​https://gitee.com/didispace/swagger-butler​

标签:Spring,boot,value,参数,Swagger2,注解,import,springfox,id
From: https://blog.51cto.com/u_10176086/5951823

相关文章

  • spring boot踩坑日记——idea找不到配置文件
    报错:  原因springboot启动时找不到配置文件可以看出配置文件图标显示不对: 解决:标注配置文件......
  • 亲手实现一个springboot默认配置&起步加载
    实现一、默认配置1、创建springboot项目引入spring-boot-dependencies依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-......
  • 【java】(二) SpringBoot 源码解析——run启动加载过程——准备环境
    1.前言深入学习springboot笔记系列,可能会有错误还请指正,互相勉励,互相学习。上一章讲了SpringApplicaiton是如何初始化的,本章讲解后续的run方法的启动过程。本章涉及......
  • spring boot 环境下使用logback
    一、logback的介绍    Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback-classic和logback-access。logback-core是其......
  • spring boot —— 整合Scheduling定时任务
    Spring3.0后提供SpringTask实现任务调度,支持按日历调度,相比Quartz功能稍简单,但是在开发基本够用,支持注解编程方式。使用启用在springboot启动类上添加注解:@EnableSch......
  • SpringMVC核心技术
    入门程序Web.xml(前端控制器)配置:        在web.xml中添加DispatcherServlet的配置。         <servlet><servlet-name>springmvc</servlet......
  • 本地搭建bootlin elixir查阅内核代码
    转自:https://barryx.cn/build_bootlin_elixir平时经常使用elixir.bootlin.com查看内核源码,很方便。但是苦于该站点服务器在国外,国内用起来很卡很慢,所以想着自己在本地搭......
  • SpringBoot3.x中spring.factories功能被移除的解决方案
    背景笔者所在项目组在搭建一个全新项目的时候选用了​​SpringBoot3.x​​​,项目中应用了很多​​SpringBoot2.x​​​时代相关的第三方组件例如​​baomidou​​​出品的​......
  • Spring Boot 之 RESRful API 权限控制
    一、为何用RESTfulAPI1.1RESTful是什么?RESTful(RepresentationalStateTransfer)架构风格,是一个Web自身的架构风格,底层主要基于HTTP协议(ps:提出者就是HTTP协议的作者),是分布......
  • Spring的两种任务调度Scheduled和Async
    Spring提供了两种后台任务的方法,分别是:调度任务,@Schedule异步任务,@Async当然,使用这两个是有条件的,需要在spring应用的上下文中声明​​​<task:annotation-driven/>​​​......