400 Bad Request when getting v3/api-docs

huangapple go评论69阅读模式
英文:

400 Bad Request when getting v3/api-docs

问题

I am using springdoc-openapi-starter-webmvc-ui to render my API schema (Spring Boot 3).

我正在使用springdoc-openapi-starter-webmvc-ui来渲染我的API架构(Spring Boot 3)。

I annotated my controller class:

我为我的控制器类添加了注解:

When my application is running I can access http://localhost:8080/swagger-ui/index.html, but unfortunately it shows Failed to load API definition. with error:

当我的应用程序在运行时,我可以访问http://localhost:8080/swagger-ui/index.html,但不幸的是,它显示Failed to load API definition.并显示错误:

Any ideas what's wrong with my code?

对于我的代码有什么问题有什么想法?

英文:

I am using springdoc-openapi-starter-webmvc-ui to render my API schema (Spring Boot 3).

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
    <version>2.0.2</version>
</dependency>

I annotated my controller class:

@OpenAPIDefinition(
    info = @Info(
        title = "Test Application",
        description = "Description"
    )
)
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
public class TestController {

    @Operation(summary = "Test endpoint")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "204", description = "OK", content = @Content),
        @ApiResponse(responseCode = "400", description = "Invalid request", content = @Content),
        @ApiResponse(responseCode = "500", description = "Internal server error", content = @Content)})
    @PostMapping("/test")
    public ResponseEntity<Object> test(@RequestBody @Valid @ParameterObject TestRequest testRequest, BindingResult bindingResult) {
        ...
    }
}

When my application is running I can access http://localhost:8080/swagger-ui/index.html, but unfortunately it shows Failed to load API definition. with error:

Fetch error
response status is 400 /v3/api-docs

Any ideas what's wrong with my code?

答案1

得分: 0

我已成功解决了这个问题。原来是我的一个依赖项使用了旧版本的swagger,所以我不得不将其排除:

<exclusion>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-core</artifactId>
</exclusion>
英文:

I've managed to resolve the problem. It turned out that one of my dependency used an old version of swagger, so I had to exclude it:

&lt;exclusion&gt;
    &lt;groupId&gt;io.swagger.core.v3&lt;/groupId&gt;
    &lt;artifactId&gt;swagger-annotations&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;exclusion&gt;
    &lt;groupId&gt;io.swagger.core.v3&lt;/groupId&gt;
    &lt;artifactId&gt;swagger-core&lt;/artifactId&gt;
&lt;/exclusion&gt;

huangapple
  • 本文由 发表于 2023年3月9日 22:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定