swagger-ui
path customization
API documentation
web development
swagger configuration

How to change swagger-ui.html default path

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing the default Swagger UI path depends on which library is serving Swagger UI in your application. In modern Spring Boot projects using springdoc-openapi, the path is configurable with a property. In older Springfox-based setups, you often need a redirect or custom resource mapping instead.

Identify which Swagger library you are using

The path behavior is not universal because swagger-ui.html is a convention provided by a specific integration library, not by Spring Boot itself.

Two common cases are:

  • 'springdoc-openapi, which usually supports property-based path customization'
  • Springfox, which historically exposed /swagger-ui.html with less direct configurability

The correct solution depends on which of those is in your dependency graph.

With springdoc-openapi, use the built-in property

If your project uses springdoc-openapi, you can change the UI path in application.properties:

properties
springdoc.swagger-ui.path=/docs

Or in YAML:

yaml
springdoc:
  swagger-ui:
    path: /docs

After that, the UI is served at /docs instead of the default path. This is the cleanest modern answer because it is supported directly by the library.

With Springfox, a redirect is often the practical answer

Older Springfox setups commonly exposed the UI at /swagger-ui.html and did not always provide a simple one-line property to rename it. In that case, the common workaround is to create a controller or view-controller redirect:

java
1import org.springframework.stereotype.Controller;
2import org.springframework.web.bind.annotation.GetMapping;
3
4@Controller
5public class SwaggerRedirectController {
6
7    @GetMapping("/docs")
8    public String redirectToSwaggerUi() {
9        return "redirect:/swagger-ui.html";
10    }
11}

This does not rename the underlying resource. It gives your application a friendlier custom route.

A WebMvcConfigurer redirect keeps things centralized

If you prefer a configuration-based approach:

java
1import org.springframework.context.annotation.Configuration;
2import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
3import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
4
5@Configuration
6public class SwaggerPathConfig implements WebMvcConfigurer {
7    @Override
8    public void addViewControllers(ViewControllerRegistry registry) {
9        registry.addRedirectViewController("/docs", "/swagger-ui.html");
10    }
11}

This is useful when you want the path customization in one place rather than in a dedicated controller.

Remember that path changes are not a security control

Teams sometimes change the Swagger UI path for "security." That can reduce accidental discovery, but it is not real access control. If the docs should be restricted, protect them with authentication, authorization, or network-level controls.

Changing /swagger-ui.html to /docs can improve URL consistency. It should not be treated as a substitute for security configuration.

Test the generated assets after changing the route

After changing the entry path, open the UI in a browser and verify that the static assets and OpenAPI document still load correctly. A redirect that reaches the HTML page but breaks JavaScript bundles or the API docs URL is only a partial success.

Update reverse proxies and bookmarks too

Once the path changes, make sure related pieces stay aligned:

  • reverse-proxy routing rules
  • API gateway path rewrites
  • developer documentation
  • monitoring checks that probe the docs endpoint

A path change is easy in code and easy to forget everywhere else.

Common Pitfalls

  • Applying springdoc.swagger-ui.path in a project that actually uses Springfox.
  • Assuming a new path automatically secures the documentation endpoint.
  • Changing the route in the app but forgetting proxy or gateway rules.
  • Redirecting to /swagger-ui.html without verifying that the underlying resource is still enabled.
  • Mixing examples from different Swagger integration libraries.

Summary

  • The right way to change the Swagger UI path depends on the library serving it.
  • 'springdoc-openapi supports direct property-based path customization.'
  • Older Springfox setups often need a redirect or custom MVC mapping instead.
  • Path changes improve organization, but they do not provide real security.
  • After changing the route, update proxies, docs, and any monitoring that depends on the old path.

Course illustration
Course illustration

All Rights Reserved.