在 Swagger UI 中看不到来自我的控制器服务的请求

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

我知道这个问题已经被问过很多次了。但是,经过多次尝试,我还没有找到解决问题的方法。

我有一个 Java 项目(v17),使用 Spring、MongoDB 和 Swagger 来允许我的前台和后台之间进行服务和交换。

但是,我在 SWAGGER 的 GUI 中看不到我的控制器的方法。

这是我的代码:

运行类:

package demomongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@SpringBootApplication
public class Run implements CommandLineRunner {
    
    @Autowired
    private MongoDBExample mongoDBExample;

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

    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        
    }

}

我的招摇配置:

package demomongo;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig 
{
   @Bean
   public Docket api() 
   {
       return new Docket(DocumentationType.SWAGGER_2)
               .select()
.apis(RequestHandlerSelectors.basePackage("com.web.controller"))
               .paths(PathSelectors.any())
               .build();
   }
   
   private ApiInfo apiInfo() {
       return new ApiInfoBuilder()
               .title("Your API Title")
               .description("Your API Description")
               .version("1.0.0")
               .build();
   }
 }

我的控制器:

    package com.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api
public class Controller {
    
      @Autowired
        public Controller() {
            
        }

    @ApiOperation(value = "Dit bonjour")
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello World!";
    }

    @RequestMapping(value = "/custom", method = RequestMethod.POST)
    public String custom() {
        return "custom";
    }

}

我的 POM.XML :

<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 http://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>2.5.0</version>
</parent>

<groupId>demomongo</groupId>
<artifactId>demomongo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>17</java.version>
    <springfox.version>3.0.0</springfox.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.4</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

谢谢!

java spring-boot swagger swagger-ui java-18
© www.soinside.com 2019 - 2024. All rights reserved.