0
votes

I am trying to generate Swagger documentation from a springboot project using Springfox and following https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api documentation.

Initially I got the error "Full authorization is required to access the resource" since I am using OAuth2 in my application. I changed the configuration to permit all the requests ending with /swagger-ui.html.

Now I have been getting "WhiteLabel error page - This application has no explicit mapping for /error" while trying to access /swagger-ui.html on my local.

I went through various posts but none of the solutions worked for me - I am not using @webmvcconfiguration which can interfere. Can anyone help?

4

4 Answers

2
votes

For Swagger 3.0, The URL is changed to

http://localhost:8080/swagger-ui/index.html

2
votes

This is how I solved my problem. Here is my detailed code, if someone want to look.

https://github.com/xbox2204/SpringBoot-JPA-Swagger

Now, I used 3.0.0-SNAPSHOT and a latest spring-boot starter project I created from here:

https://start.spring.io/

  1. My pom.xml, I added following dependencies:
    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.plugin</groupId>
                    <artifactId>spring-plugin-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

  1. In my application.properties, I added following:
spring.resources.add-mappings=true
  1. In my SpringBoot Main/Runner class, I added these annotations
@EnableWebMvc
@EnableSwagger2
@SpringBootApplication
  1. My Docket returning function looked like this
@Bean
    public Docket productApi() {
        Contact contact =new Contact(
                "Vineet Mishra",
                "https://github.com/xbox2204",
                "[email protected]"
        );
        ApiInfo apiInfo= new ApiInfoBuilder().title("VINEET SPRING-BOOT API")
                .description("Spring-Boot for all")
                .termsOfServiceUrl("jUST CHILL!!!")
                .contact(contact)
                .licenseUrl("[email protected]").version("1.0").build();

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build();
    }
  1. Finally, I accessed my swagger-ui from

http://localhost:8080/swagger-ui/index.html#

Image of final result

1
votes
  1. Remove the v2 dependencies from your pom.xml or comment them out.

     <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger2</artifactId>
         <version>3.0.0</version>
     </dependency>
    
     <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>3.0.0</version>
     </dependency>
    
  2. Add the springfox-boot-starter

     <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-boot-starter</artifactId>
         <version>3.0.0</version>
     </dependency>
    
  3. Change the URL in your browser to: http://localhost:8080/swagger-ui/index.html

The general form for the URL is:

  • http://host/context-path/swagger-ui/index.html OR
  • http://host/context-path/swagger-ui/

For more info check this link that leads to the relevant documentation: https://springfox.github.io/springfox/docs/snapshot/#changes-in-swagger-ui

0
votes

The swagger-ui.html page makes a number of calls to get all the details. If you use your browser's dev tools (usually just press F12 to open) you will see failing requests in the network tab. You'll need to permit requests to

    "/v2/api-docs",
    "/swagger-resources/**",
    "/swagger-ui.html**",
    "/webjars/**"

There's some info in the springfox docs , do a find for 'security' or 'authorization'