Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make default url to be viewed in browser in spring boot?

I have a microservice developed using Spring Boot 2.1.3 version and also I have used SpringFox version 2.9.2 for Swagger documentation. Every time when I distribute or deploy to third party or any other person, I have to always mention the swagger url for the user to go through the REST end-points. My question is how to make a default redirected url in case of spring boot so that it should redirect to swagger-ui.html automatically. It means if the user types http://localhost:8080 in the browser, the browser should automatically redirect to the url ie. http://localhost:8080/api/swagger-ui.html. I want to know is there any configuration required for this ?

Before reaching to stackoverflow, I have gone through the following links and tried, but nothing worked as expected.

Java Spring Boot: How to map my app root (“/”) to index.html?

Changing default welcome-page for spring-boot application deployed as a war

I tried different ways also, but I always get 404 or Whitelabel Error Page. I want to know is there any way in case whitelabel error page it should automatically redirect to swagger page ie. http://localhost:8080/api/swagger-ui.html.

I have also added the below in application.properties.

server.servlet.context-path=/api

Please help me in this regard.

like image 918
Sambit Avatar asked Nov 03 '25 08:11

Sambit


1 Answers

You could add a RedirectViewController like this:

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

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

}
like image 157
Johannes Erchen Avatar answered Nov 05 '25 22:11

Johannes Erchen