Search code examples
spring-bootswagger-uispringfox

Getting 405 - Method Not Allowed in Swagger (Spring Boot)


I'm getting a 405 - Method Not Allowed error when testing a GET method in Postman. In a browser, when trying to use swagger-ui I'm getting a Whitelabel Error Page.

I'm on Java 17, Spring Boot 2.7.3, and I'm using Swagger 3.0.

Here's the Swagger configuration. I've had to remove @Configuration because Spring Boot couldn't start when that annotation was in use and DocumentationType.OAS_30 is here instead of SWAGGER_2 because I'm using Swagger 3.0.

package hr.ogcs.blueprint;

import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30).select()
                .apis(RequestHandlerSelectors.basePackage("hr.ogcs.blueprint"))
                .paths(PathSelectors.any()).build();
    }
}

Here's a Gradle configuration. I've tried with springfox-boot-starter dependency, but the Spring Boot couldn't start with it. So I added swagger2 and swagger-ui dependencies.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.springfox:springfox-swagger2:3.0.0'
    implementation 'io.springfox:springfox-swagger-ui:3.0.0'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

enter image description here


Solution

  • Here's a solution:

    In order to solve this issue, I dropped the Springfox library and used another library named SpringDoc. The Springfox obviously still doesn't work with Swagger 3.0 so remove this from your Gradle:

    implementation 'io.springfox:springfox-swagger2:3.0.0'
    implementation 'io.springfox:springfox-swagger-ui:3.0.0'
    

    The solution is quite simple. Use this dependency instead, and everything will work like a charm:

    `implementation` 'org.springdoc:springdoc-openapi-ui:1.6.11'
    

    You don't need any additional Swagger Config file or any annotation. Now go to http://localhost:8080/swagger-ui.html to see the results.

    I've followed this tutorial on Baeldung.