2
votes

First of all, in advance, thanks for all the replies.

Now let's move on to the problem. Today, i installed all the 3.0.0 Swagger dependencies for a Spring Boot project (boot-starter, swagger2 and swagger-ui). Everything is working as it should, but i can't change the apiInfo informantion in the Docket builder.

Gradle:

    implementation "io.springfox:springfox-boot-starter:3.0.0"
    implementation "io.springfox:springfox-swagger2:3.0.0"
    implementation "io.springfox:springfox-swagger-ui:3.0.0"

SpringFoxConfig.java:

package io.ubivis.swagger;

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;

@Configuration
public class SpringFoxConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(metaData());
    }

    private ApiInfo metaData() {
        return new ApiInfoBuilder()

                .title("Spring Boot REST API")
                .description("Spring Boot REST API for Space Study")
                .version("1.0.0")
                .license("Apache 2.0")
                .contact(new Contact("XYZ", "XYZ.com", "[email protected]"))
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                .build();
    }
}

Even with the metaData function being called in the Docket builder, Swagger just ignores it and doesn't change any of the default information.

Does anyone knows how to fix this? Thanks!

Edit:

Just realized that I can't change anything of the default values. Even when I try to change the GlobalResponseMessage:

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.POST, responseMessageForGet())
                .apiInfo(metaData());
    }

Nothing happens and the page keep showing the default globalResponseMessage.

In this edit, I tried to downgrade the dependency to version 2.9.2, seeking where could be my problem. But I do still have the same problem (and now another one lol).

1
Hi, shouldn't you better call .build() method at end ? I think paths are well builded but defaultResponse, globalResponse and apiInfo remains to default.Gweltaz Niquel
Hi, thanks for the response! Yeah, I think they should be before the .build() method in versions 2.X.X. I probably made a mistake because version 3.0.0 of Swagger shifts those functions to be called after the build. Unfortunately, this doesn't solve the problem, but great point about this version problem.vicnetto

1 Answers

3
votes

Just found the answer for my problem. Actually, at the beginning, I forgot to mention that my project was a multimodule spring project. I didn't thought this would change anything on the problem, but I was wrong. The problem was that my SpringApplication wasn't scanning the class SwaggerConfiguration.

If you're having any problem like that, you should insert this annotations into your SpringBootApplication:

@SpringBootApplication(scanBasePackages = "br.com.vicnetto")
@EnableSwagger2
public class SpringBootApplication {

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

The argument scanBasePackages of the @SpringBootApplication should include the SwaggerConfiguration class.

And the second annotation @EnableSwagger2 needs to be in the @SpringBootApplication context (only for Swagger version under 3.0.0).