我正在尝试将 Swagger 添加到我的项目https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
我在上面的教程中得到了 4.3 段落,当我运行我的应用程序时,我遇到了一些错误。
我将其添加到我的pom.xml:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
另外,我添加了配置类:
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的项目时,我得到:
我正在尝试使用以下代码将 springfox 迁移到 springdoc:
import io.swagger.annotations.ApiModelProperty;
@ApiModelProperty(position = 30, required = true, value = "Group value")
Run Code Online (Sandbox Code Playgroud)
到
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(position = 20, required = false)
Run Code Online (Sandbox Code Playgroud)
但我找不到position和value。你知道在 springdoc 中替换它们的正确方法是什么吗?
从swagger.io编辑器页面下载了Swagger宠物商店(简单)并做了一个mvn jetty:run
在Chrome上打开DevTools并执行GET时,我没有看到标题Content-Type字段被设置.
这是在GET/pets {id}

我们正在使用spring控制器来处理文件上传:
例如:
@RequestMapping(value = "/scan", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ScanResult scan(HttpServletRequest request) throws IOException, FileUploadException {
return scanService.scanFile(parseMultipart(request));
}
Run Code Online (Sandbox Code Playgroud)
但是我们没有使用任何多部分解析器,而是从servlet请求输入流中流式传输文件。由于性能原因,我们需要立即开始处理文件。
以这种方式执行操作时,我们似乎无法对多部分文件使用典型的检测/配置。我知道Springfox(我们用于生成我们的swagger文档)会在将MultipartFile作为控制器参数的情况下生成适当的swagger控件,但对我们而言并非如此。
还有其他配置选项可用来提示springfox我们要在此处上传文件吗?
我有一个REST API和包含的springfox swagger v2.6.1并可以正常工作。但是现在,我不想总是显示我拥有的所有控制器,因为其中一些控制器非常技术性,不适合普通用户使用,但是我希望能够选择显示的内容而不必重新编译代码。页面顶部有一个下拉字段,上面写着“默认(/ v2 / api-docs)”(或您配置的任何内容),仅此一项。我的直觉是应该在那里可以有多个选项,并根据该选项显示或不显示某些控制器类。
由于我真的不知道如何上传图像,因此无法提供屏幕截图。我希望我的问题仍然清楚。
在我的项目中大张旗鼓的代码是最简单的:
@Bean
public Docket api() {
return new Docket( DocumentationType.SWAGGER_2 )
.select()
.apis( RequestHandlerSelectors.any() )
.paths( PathSelectors.any() )
.build()
.apiInfo( metadata() );
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title( "My awesome ACS API" )
.description( "All the requests that the server will respond to." )
.version( "1.0.0" )
.build();
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法,例如添加一些属性,执行两个.select()并选择不同的东西,但是我似乎并没有真正达到我希望实现的目标。
谢谢你的帮助!
我在代码中有 Springfox 注释如下:
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Options for the endpoint", responseHeaders = {@ResponseHeader(name = "Allow", description = "Verbs allowed")})})
Run Code Online (Sandbox Code Playgroud)
但是,标头不会在 Swagger UI 中的响应下方呈现。
如果我通过 Docket 添加全局响应(针对内部服务器错误),则其标头呈现得很好。
这是配置错误还是这里有什么问题?
我有一个控制器,必须通过使其接受Stringas @RequestBody并返回String 来保持通用性,即String processRequest(@RequestBody String json) {...}
我无法控制该控制器的源代码,但可以通过编程来实现。
这会在传递和返回的真正对象是在一系列的请求消息的其他地方定义:RequestMessage1.java,RequestMessage2.java,等反应同样是:Response1.java,Response2.java1。
该控制器还插手这些请求的处理,以一个Processor看起来是这样的(Request1Processor.java)Response1 process(RequestMessage1 message)。
我的问题是这个。
有没有一种方法可以配置swagger,使其公开REST控制器类的终结点,即processRequest,但将所有这些Processor类及其输入和输出显示为该控制器的文档?
作为文档的一部分,我看到了添加“不可达”模型的功能。我尝试了像这样的文档中的方法:
@Autowired
private TypeResolver typeResolver;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.additionalModels(typeResolver.resolve(Date.class));
}
Run Code Online (Sandbox Code Playgroud)
但是,附加的Date模型没有显示在swagger-ui.html中。
我在这里做错了什么?
此外,是否有办法以某种方式显示RequestMessage1类型将对做出响应Response1?
我有一个正在使用Springfox生成API文档的项目。
我想在编译时生成swagger.json 。
以下是示例springfox配置,
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.abc.xyz"))
.paths(regex("/*.*"))
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
仅供参考:我也尝试过https://github.com/kongchen/swagger-maven-plugin插件,但是它不起作用
我正在使用 Swagger 注释和 SpringFox 为我的 REST API(使用 Sprint Boot 构建)生成 Swagger 规范。我用@ApiResponse将返回的代码注释每个方法。例如:
@DeleteMapping("/{pipelineName}")
@ApiOperation(value = "Delete a specific pipeline")
@ApiResponses({
@ApiResponse(code = 204, message = "Pipeline has been deleted"),
@ApiResponse(code = 404, message = "Pipeline does not exist")
})
public void deletePipeline(@PathVariable("pipelineName") @ApiParam(value = "Name of pipeline", required = true) String name){
...
}
Run Code Online (Sandbox Code Playgroud)
但是,有些东西(我假设 SpringFox)会为每个 API 调用添加一个 200 响应代码,无论它是否定义。我知道我可以通过向@ResponseStatus每个方法添加注释来删除它,但是 a) 这似乎是 @ApiResponse 定义的不必要重复,并且 b) 某些方法可能返回多个 2xx 代码之一。
有没有办法或者去掉默认添加的200代码?
我有一个 Spring Boot (kotlin) 项目,我使用 springdoc-openapi 来生成 OpenApi 3 规范。我的数据模型如下所示:
open class Animal
data class Cat(val catName: String) : Animal()
data class Dog(val dogName: String) : Animal()
open class Food<T : Animal>
class CatFood : Food<Cat>()
class DogFood : Food<Dog>()
Run Code Online (Sandbox Code Playgroud)
和一个像这样的简单控制器:
@GetMapping("/test")
fun test(): Food<out Animal> = DogFood()
Run Code Online (Sandbox Code Playgroud)
生成的 yaml 是:
openapi: 3.0.1
info:
title: OpenAPI definition
version: v0
servers:
- url: http://localhost:8085
paths:
/test:
get:
tags:
- test-controller
operationId: test
responses:
"200":
description: default response
content:
'*/*':
schema:
$ref: …Run Code Online (Sandbox Code Playgroud) springfox ×10
swagger ×8
java ×6
spring-boot ×4
spring ×2
springdoc ×2
openapi ×1
spring-mvc ×1
swagger-ui ×1