How to make default url to be viewed in browser in spring boot?
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";
}
Related videos on Youtube
Comments
-
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 about 5 yearsI have already tried this option, I have also mentioned in the post. It is not working.
-
Al-Amin about 5 yearstake a look here hope you will find solution docs.spring.io/spring-data/rest/docs/current/reference/html/…
-
Bwvolleyball about 5 years@debadatta-mishra, you'll need to either delete your
server.servlet.context-path=/api
or you'll have to hitlocalhost:8080/api
for this redirect to work. Based on your question though, it looks like you really don't want thatserver.servlet.context-path
property set, because then your web service is only accessible from/api/
, and never just/
-
Sambit about 5 years@Bwvolleyball, I have already tried these options, nothing worked.
-
Sambit about 5 yearsHi 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 about 5 yearsIf 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 about 5 yearsHi 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 about 5 yearsthan maybe you should check your configuration, in a blank application this will work ..
-
Vikram kumar Chhajer about 2 yearsI tried this, but somehow my https request is going to http URL