无法使用 Spring Boot 应用程序设置 swagger

问题描述 投票:0回答:1

我与 gradle 一起创建了一个 Spring Boot 应用程序。 我添加了以下 gradle 依赖项:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.springfox:springfox-boot-starter:3.0.0'
    implementation 'io.springfox:springfox-swagger-ui:3.0.0'
    
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

然后我添加了一个控制器:

package com.google.demoapp.controller;

import org.springframework.web.bind.annotation.*;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
@RestController
@RequestMapping("/api")
@Tag(name = "Message API")
public class MessageController {

    @GetMapping("/message")
    @Operation(summary = "Get a message")
    public String getMessage() {
        return "Hello, this is a GET request!";
    }

    @PostMapping("/message")
    @Operation(summary = "Post a message")
    public String postMessage(@RequestBody String message) {
        return "You posted: " + message;
    }
}

然后我添加了 swaggerConfig 类

package com.google.demoapp.config;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
//@EnableSwagger2 // You can remove this annotation for Swagger 3
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30) // Use OAS_30 for Swagger 3
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.demoapp")) // Replace with your package name
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("My RESTful Service")
                .description("A simple RESTful service with Swagger documentation")
                .version("1.0")
                .contact(new Contact("Your Name", "", "y[email protected]"))
                .build();
    }
}

我正在使用以下方式构建代码:gradelw clean build -x test

我正在使用以下代码运行代码:gradlew bootRun

此后,当我尝试使用 http://localhost:8080/swagger-ui.html 访问 swagger 时

我收到以下错误:

请帮我解决这个错误。

java spring spring-boot swagger-ui
1个回答
0
投票

您使用 Swagger ui 3。您不创建配置文件。 https://www.baeldung.com/spring-rest-openapi-documentation。你必须一步一步地遵循。如果创建配置文件,你应该大摇大摆 2

© www.soinside.com 2019 - 2024. All rights reserved.