spring-bootswagger-uispringfoxspringfox-boot-starter

Can I get /swagger-ui.html to redirect to /swagger-ui/


At springfogx-swagger-ui 3.0.0 my Spring Boot app ends up with the Swagger UI at http://myapp.example.com:8080/swagger-ui/. My users are used to seeing that URL as http://myapp.example.com:8080/swagger-ui.html. Is there a way I can set up a redirect or something to let users who "know" http://myapp.example.com:8080/swagger-ui.html be routed to the "correct" URL http://myapp.example.com:8080/swagger-ui/?

I got both swagger and swagger-ui in the "usual" Spring Boot manner

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

This may be a more Spring Boot question than a Spring Fox question in any event. A redirect seems like the no-muss-no-full solution.


Solution

  • Meh. This works.

    
    /**
     * Redirects requests for swagger-ui.html to the /swagger-ui/ endpoint.
     */
    @ApiIgnore
    @RestController
    public class SwaggerHtmlRedirector {
      @RequestMapping(
          method = RequestMethod.GET,
          path = "/swagger-ui.html")
      public RedirectView redirectWithUsingRedirectView() {
        return new RedirectView("/swagger-ui/");
      }
    }
    

    Source is https://www.baeldung.com/spring-swagger-hiding-endpoints

    Is there anything better?