2

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.

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115

2 Answers2

3

Yes, the redirect seems like the simplest approach. If you already have a custom MVC configuration then you can add the addRedirectViewController line.

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class MyWebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/swagger-ui.html", "/swagger-ui/");
    }

}
Ezra Katz
  • 116
  • 8
2

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?

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115