1

I created an example of Spring Boot 3 with usage of swagger 2.

After I ran the app, I tried to open swagger ui through this URL (http://localhost:8080/swagger-ui.html) but I got "Whitelabel Error Page" result.

Here is swagger config shown below

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.deprem"))
                .paths(PathSelectors.regex("/.*"))
                .build().apiInfo(apiEndPointsInfo());

    }
    private ApiInfo apiEndPointsInfo() {
        return new ApiInfoBuilder().title("Title")
                .description("Description")
                .contact(new Contact("Name and Surname", "", ""))
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .version("1.0.0")
                .build();
    }
}

Here are dependencies used for swagger ui in pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>


    </dependencies>

Here is the console output shown below.

Link

Here is the log info shown below

2023-02-09T01:08:16.232+03:00  INFO 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-02-09T01:08:16.232+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
2023-02-09T01:08:16.232+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected AcceptHeaderLocaleResolver
2023-02-09T01:08:16.232+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected FixedThemeResolver
2023-02-09T01:08:16.233+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@53b689a4
2023-02-09T01:08:16.233+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.support.SessionFlashMapManager@609759ed
2023-02-09T01:08:16.233+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2023-02-09T01:08:16.233+03:00  INFO 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2023-02-09T01:08:16.239+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/swagger-ui.html", parameters={}
2023-02-09T01:08:16.245+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-02-09T01:08:16.250+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2023-02-09T01:08:16.250+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2023-02-09T01:08:16.254+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2023-02-09T01:08:16.255+03:00 DEBUG 24272 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2023-02-09T01:08:16.276+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2023-02-09T01:08:16.281+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

Here is my application.properties file shown below

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

logging.level.org.springframework.web=DEBUG
logging.level.io.springfox=DEBUG

How can I fix the issue?

Here is my repo : Link

S.N
  • 2,157
  • 3
  • 29
  • 78
  • Any error in the back end? Any stacktrace? – Jorge Campos Feb 08 '23 at 20:30
  • @JorgeCampos Here is the screenshot : https://ibb.co/Xfnfm9r . There is no error in the console. – S.N Feb 08 '23 at 20:36
  • 1
    Please don't use images. Stacktrace is mainly text. Copy it and add it to your question. Besides, not clicking in a link without knowing its procedence. – Jorge Campos Feb 08 '23 at 21:20
  • @JorgeCampos Ok. I did that. Where is the problem? – S.N Feb 08 '23 at 21:32
  • The console output you show is only showing the startup of the application, does it not change after you call the page where you see the error on the browser? If not, set your application to DEBUG or TRACE mode it will likely show what went wrong. And again, if there is a Stacktrace, copy the text and past it formatted in your question. It is easier for anyone that wants to help to be able to copy the information from text and investigate. – Jorge Campos Feb 08 '23 at 21:46
  • @JorgeCampos I shared the logs. Can you look through it? – S.N Feb 08 '23 at 22:12
  • The page you are trying to access does not exist in your application [404 error](https://en.wikipedia.org/wiki/HTTP_404) means page not found. That means something on your setup is not correct because the Swagger application. You probably are missing some configuration that tells the Swagger library to how to render the request to swagger-ui.html . You should follow the steps on their doc page: https://springfox.github.io/springfox/docs/current/#changes-in-swagger-ui – Jorge Campos Feb 08 '23 at 23:17
  • @JorgeCampos Where is the missing part in Swagger config class? – S.N Feb 08 '23 at 23:43
  • From the link I shared: "swagger-ui location has moved from **http://host/context-path/swagger-ui.html** to http://host/context-path/swagger-ui/index.html OR http://host/context-path/swagger-ui/ for short. This makes it work much better with pulling it as a web jar and turning it off using configuration properties if not needed." – Jorge Campos Feb 08 '23 at 23:55
  • @JorgeCampos I tried to use http://localhost:8080/api/v1/earthquakes/swagger-ui/index.html but I still get "Whitelabel Error Page" – S.N Feb 09 '23 at 05:01
  • Please check my answer on **[How to run Swagger 3 on Spring Boot 3](https://stackoverflow.com/questions/74614369/how-to-run-swagger-3-on-spring-boot-3)** – Murat Yıldız Mar 18 '23 at 08:58

1 Answers1

6

You need to use a different dependency for Spring Boot 3 and upgrade your Swagger logic to v3:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.2</version>
</dependency>

Example OpenAPI config:

@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI openApi() {
        return new OpenAPI()
            .info(
                new Info()
                    .title("App Title")
                    .description("App description")
                    .version("App version"));
            .externalDocs(
                new ExternalDocumentation()
                    .description("Documentation name")
                    .url("https://example.com"));
    }

    @Bean
    public GroupedOpenApi categoryApi() {
        return GroupedOpenApi.builder()
            .group("Category API")
            .pathsToMatch("/categories/**")
            .build();
    }

    @Bean
    public GroupedOpenApi expenseApi() {
        return GroupedOpenApi.builder()
            .group("Expense API")
            .pathsToMatch("/expenses/**")
            .build();
    }

}

Example controller annotations:

@RequestMapping("/categories")
@RestController
@Validated
@ApiResponse(responseCode = "401", description = "Unauthorized, login required")
@ApiResponse(responseCode = "500", description = "Internal server error, this should not happen")
@Tag(name = "Category Controller", description = "Endpoint for retrieving and modifying categories")
public class CategoryController {

    @GetMapping
    @Operation(
            summary = "Get all categories (paginated)",
            responses = @ApiResponse(responseCode = "200", description = "Category page returned"))
    public Page<CategoryDto> getCategories(@PageableDefault @ParameterObject Pageable pageable) {
        // Controller logic
    }

}

Example DTO:

@Value
public class CategoryDto {

    String id;

    @NotBlank
    @Size(max = 40, message = "must not exceed 40 characters")
    @Schema(maxLength = 40, required = true)
    String name;

    // Read-only attributes

    @Schema(accessMode = Schema.AccessMode.READ_ONLY)
    LocalDateTime createdAt;

    @Schema(accessMode = Schema.AccessMode.READ_ONLY)
    LocalDateTime lastModifiedAt;

}
times29
  • 2,782
  • 2
  • 21
  • 40