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

11,123

Solution 1

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");
    }

}

Solution 2

Your controller should like below:

@RestController
public class DefaultController implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
    public void handleErrorWithRedirect(HttpServletResponse response) throws IOException {
        response.sendRedirect("/swagger-ui.html");
    }

    @RequestMapping(value = "/")
    public void redirect(HttpServletResponse response) throws IOException {
        response.sendRedirect("/swagger-ui.html");
    }

}

I also have put together a working model for you in my github spring-boot project.
For default/index page or error page, it will always redirect to swagger-ui.html.
Let me know if you still have questions.

Solution 3

You can use a Controller for your default path

@RequestMapping("/")
public String index(Model model) {            
    return "redirect: /api/swagger-ui.html";

}

Share:
11,123

Related videos on Youtube

Sambit
Author by

Sambit

A learner and a developer

Updated on June 04, 2022

Comments

  • Sambit
    Sambit almost 2 years

    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.

  • Sambit
    Sambit about 5 years
    I have already tried this option, I have also mentioned in the post. It is not working.
  • Al-Amin
    Al-Amin about 5 years
    take a look here hope you will find solution docs.spring.io/spring-data/rest/docs/current/reference/html/‌​…
  • Bwvolleyball
    Bwvolleyball about 5 years
    @debadatta-mishra, you'll need to either delete your server.servlet.context-path=/api or you'll have to hit localhost:8080/api for this redirect to work. Based on your question though, it looks like you really don't want that server.servlet.context-path property set, because then your web service is only accessible from /api/, and never just /
  • Sambit
    Sambit about 5 years
    @Bwvolleyball, I have already tried these options, nothing worked.
  • Sambit
    Sambit about 5 years
    Hi Amith, I took your code checked,It does not work as per my question. Can you try localhost:8080 in the browser ? Does it redirect to swagger page ?It does not work for me. We can handle error page in a way.
  • Amith Kumar
    Amith Kumar about 5 years
    If you look into application.properties file, I have this setting # ****** WebServer ******* server.servlet.context-path=/ server.port=3007, so instead of 8080 if you use localhost:3007, it will work. You can change those properties as per your need.
  • Sambit
    Sambit about 5 years
    Hi Amith, if you give server.servlet.context-path=/api in application.properties, it does not work. Think that api is the name of the microservice where customer does not know. Customer simply types localhost:8080/, it should redirect to the url which I have mentioned.
  • Johannes Erchen
    Johannes Erchen about 5 years
    than maybe you should check your configuration, in a blank application this will work ..
  • Vikram kumar Chhajer
    Vikram kumar Chhajer about 2 years
    I tried this, but somehow my https request is going to http URL