如何修复 Spring Boot 3 中的 swagger-ui 问题?

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

我可以看到“http://localhost:8080/v3/api-docs”,但看不到“http://localhost:8080/swagger-ui.html”。我尝试了几个不同的网址,但没有成功。我遇到 404(Whitelabel 错误页面)

相关代码如下。我想我使用了正确的版本。我不明白它怎么不起作用。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nahrok</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <url/>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.6.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.6.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.34</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.6.0</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>0.2.0</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

spring.application.name=demo
server.port=8080

spring.datasource.url=jdbc:h2:file:~/Documents/workspace/db/tour
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

示例 RestController 类

package com.nahrok.demo.controller;


import com.nahrok.demo.model.station.StationCreateRequest;
import com.nahrok.demo.model.station.StationDetail;
import com.nahrok.demo.service.IStationService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("station")
public class StationController {
    private final IStationService stationService;

    public StationController(IStationService stationService) {
        this.stationService = stationService;
    }

    @PostMapping("/create")
    public Long create(@RequestBody StationCreateRequest request) {
        return stationService.create(request);
    }

    @GetMapping
    public StationDetail stationDetail(@RequestParam("stationId") Long stationId) {
        return stationService.getStation(stationId);
    }

    @GetMapping("/stations")
    public List<StationDetail> stations() {
        return stationService.getStations();
    }
}

SpringBootApplication类:

package com.nahrok.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

我只有一个配置类,这是:

package com.nahrok.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*");
    }
}

最后一个是控制台输出:

2024-09-24T06:16:29.157+03:00  INFO 23908 --- [demo] [           main] com.nahrok.demo.DemoApplication          : Starting DemoApplication using Java 17.0.12 with PID 23908 (USERUSERUSER\target\classes started by user in USERUSERUSER\IdeaProjects\demo)
2024-09-24T06:16:29.158+03:00  INFO 23908 --- [demo] [           main] com.nahrok.demo.DemoApplication          : No active profile set, falling back to 1 default profile: "default"
2024-09-24T06:16:29.492+03:00  INFO 23908 --- [demo] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-09-24T06:16:29.533+03:00  INFO 23908 --- [demo] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 37 ms. Found 8 JPA repository interfaces.
2024-09-24T06:16:29.755+03:00  INFO 23908 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-09-24T06:16:29.760+03:00  INFO 23908 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-09-24T06:16:29.761+03:00  INFO 23908 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-09-24T06:16:29.789+03:00  INFO 23908 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-09-24T06:16:29.789+03:00  INFO 23908 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 608 ms
2024-09-24T06:16:29.855+03:00  INFO 23908 --- [demo] [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2024-09-24T06:16:29.883+03:00  INFO 23908 --- [demo] [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.5.2.Final
2024-09-24T06:16:29.899+03:00  INFO 23908 --- [demo] [           main] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled
2024-09-24T06:16:30.047+03:00  INFO 23908 --- [demo] [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2024-09-24T06:16:30.060+03:00  INFO 23908 --- [demo] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-09-24T06:16:30.165+03:00  INFO 23908 --- [demo] [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:file:~/Documents/workspace/db/tour user=SA
2024-09-24T06:16:30.166+03:00  INFO 23908 --- [demo] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-09-24T06:16:30.177+03:00  WARN 23908 --- [demo] [           main] org.hibernate.orm.deprecation            : HHH90000025: H2Dialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default)
2024-09-24T06:16:30.675+03:00  INFO 23908 --- [demo] [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2024-09-24T06:16:30.697+03:00  INFO 23908 --- [demo] [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2024-09-24T06:16:30.821+03:00  INFO 23908 --- [demo] [           main] o.s.d.j.r.query.QueryEnhancerFactory     : Hibernate is in classpath; If applicable, HQL parser will be used.
2024-09-24T06:16:31.046+03:00  WARN 23908 --- [demo] [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2024-09-24T06:16:31.263+03:00  INFO 23908 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-09-24T06:16:31.268+03:00  INFO 23908 --- [demo] [           main] com.nahrok.demo.DemoApplication          : Started DemoApplication in 2.293 seconds (process running for 2.561)
java spring spring-boot swagger swagger-ui
1个回答
0
投票

步骤好像不对。您可以尝试以下方法。

  1. 在application.yml中添加配置
springdoc:
  swagger-ui:
    path: /swagger-ui.html
  1. 使用@EnableOpenApi注释应用程序类

    包 com.nahrok.demo;

    导入org.springframework.boot.SpringApplication; 导入 org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication @EnableOpenApi 公共类 DemoApplication {

     public static void main(String[] args) {
         SpringApplication.run(DemoApplication.class, args);
     }
    

    }

  2. 在代码中添加注释

    @RestController @Api(标签=“站”) @RequestMapping(“车站”) 公共类StationController { 私人最终的IStation服务站服务;

     @ApiOperation(value = "xxx", notes="xxx")
     public StationController(IStationService stationService) {
         this.stationService = stationService;
     }
    
  3. 运行您的项目

  4. 试试这个网址

http://localhost:8080/swagger-ui/index.html

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