3

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
  • 7,625
  • 7
  • 34
  • 65

3 Answers3

10

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

}
3

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.

Amith Kumar
  • 4,400
  • 1
  • 21
  • 28
  • Hi Amith, I took your code checked,It does not work as per my question. Can you try http://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. – Sambit Mar 27 '19 at 17:37
  • 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. – Amith Kumar Mar 27 '19 at 17:41
  • 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. – Sambit Mar 27 '19 at 18:47
1

You can use a Controller for your default path

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

}

Al-Amin
  • 596
  • 3
  • 16
  • take a look here hope you will find solution https://docs.spring.io/spring-data/rest/docs/current/reference/html/#getting-started.changing-base-uri – Al-Amin Mar 26 '19 at 19:40
  • @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 `/` – Bwvolleyball Mar 26 '19 at 20:20
  • @Bwvolleyball, I have already tried these options, nothing worked. – Sambit Mar 26 '19 at 20:28