Home > other >  Swagger @OpenAPIDefinition not doing anything
Swagger @OpenAPIDefinition not doing anything

Time:09-23

so I have a simple Spring Boot REST API that works fine, now I have added some swagger dependencies, both for ui and core, and have done some testing with some tags like @ApiIgnore or @Operation and they both work fine and both are updated in the http://localhost:8080/swagger-ui/#/

Now I am trying to update the API info through the @OpenAPIDefinition tag, something like this in my application class:

@OpenAPIDefinition(
        info = @Info(
            title = "the title",
            version = "0.0",
            description = "My API",
            license = @License(name = "Apache 2.0", url = "http://foo.bar"),
            contact = @Contact(url = "http://gigantic-server.com", name = "Fred", email = "[email protected]")))

Now I have read here that the spring boot app should do a class scan and recognize the @OpenAPIDefinition bean and update the generated json in http://localhost:8080/v3/api-docs. But this is not the case, I have also tried setting that info with openapi.yaml files but also no luck.

I suspect this might have to do with my dependencies, as I am new to swagger and still a bit lost and might have mixed something up, I leave my pom.xml here:

<?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>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
          <groupId>io.swagger.core.v3</groupId>
          <artifactId>swagger-jaxrs2</artifactId>
          <version>2.1.2</version>
        </dependency>
        <dependency>
          <groupId>io.swagger.core.v3</groupId>
          <artifactId>swagger-jaxrs2-servlet-initializer-v2</artifactId>
          <version>2.1.2</version>
        </dependency>

        
        
    </dependencies>

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

</project>

I am at a bit of a loss on how to continue and don't understand how swagger can recognize the @ApiIgnore tag for example but ignore the @OpenAPIDefinition one. As I said I am new to this whole thing and stackoverflow in general, so please forgive me if I forgot to add any relevant code, thank you!

CodePudding user response:

Try removing some unnecessary dependencies:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>3.0.0</version>
</dependency>
<dependency>
  <groupId>io.swagger.core.v3</groupId>
  <artifactId>swagger-jaxrs2</artifactId>
  <version>2.1.2</version>
</dependency>
<dependency>
  <groupId>io.swagger.core.v3</groupId>
  <artifactId>swagger-jaxrs2-servlet-initializer-v2</artifactId>
  <version>2.1.2</version>
</dependency>

springfox-boot-starter should bring them in already.

If this does not work you can always do the following:

@Configuration
public class SwaggerConfiguration {

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .info(new Info().title("the title").version("0.0").description("My API")
                    .contact(new Contact().name("Fred").url("http://gigantic-server.com").email("[email protected]")));
    }

}

CodePudding user response:

So after a lot of different things I found this guide where it explains how to alter API info, in a nutshell, I added a @Api(tags = "Employee") to my EmployeeController class, then in my Application class added:

@Bean
      public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
            .apiInfo(new ApiInfoBuilder()
                .title("Employee API")
                .description("A CRUD API to demonstrate Springfox 3 integration")
                .version("0.0.1-SNAPSHOT")
                .license("MIT")
                .licenseUrl("https://opensource.org/licenses/MIT")
                .build())
            .tags(new Tag("Employee", "Endpoints for CRUD operations on employees"))
            .select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .build();
      } 

And this appears to solve the issue, I now wonder what would happen if I had multiple controllers, I hope this helps anyone that was in my predicament, the overall issue seemed to be that this is a springfox related way of altering API documentation and the previous ways are swagger-core related? As I said I am new to this world so if anyone could point me out to why it wasn't working it would be amazing, thanks!

  • Related