0

I know this question has been asked many times. However, I have not found the solution to my problem after many attempts.

I have a Java project (v17) using Spring, MongoDB and Swagger to allow services and exchanges between my front and back.

However, I can't see the methods of my controller in the GUI of SWAGGER.

Here is my code:

Run.class :

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
        
    }

}

My SwaggerConfig :

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();
   }
 }

My controller :

    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";
    }

}

My 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>

Thanks !

KRdg
  • 45
  • 7
  • 1
    I would try to remove `.apis(RequestHandlerSelectors.basePackage("com.web.controller"))` and put Controller into `demomongo` package. – PeterMmm Feb 24 '23 at 15:28

0 Answers0