I have a requirement that in case of localhost:8080 , it should point to a default web page in spring Boot application. Currently I am using Spring Boot 3.1.1. Normally everything works, but I add server.servlet.contextPath=my-service
, it does not work. I provide below the code.
My Controller Code
@RestController
public class DefaultController {
@GetMapping(path = "/")
public ResponseEntity<String> getValue() {
return ResponseEntity.ok("Hello World");
}
@GetMapping(path = "/info")
public ResponseEntity<String> getInfoValue() {
return ResponseEntity.ok("Some Information");
}
}
I have also tried with the following @Configuration
option.
@Configuration
public class WebMVCApplicationConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/my-service/info");
}
}
If I comment the following in application.properties, everything works fine.
# server.servlet.contextPath=/my-service
If I uncomment the contextPath, I get HTTP Status 404 – Not Found
Please suggest me. I want to redirect to a web page along with contextPath. If someone is accessing localhost:8080
, it should redirect to localhost:8080/my-service/info
.
Please help me.