1

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.

Deba
  • 859
  • 5
  • 9
  • 21
  • Remove `@EnableWebMvc` (this disables large parts of the auto-configuration of SPring Boot) and your controller needs either a `@RestController` instead of `@Controller` or `@ResponseBody` on the methods as you aren't returning a model/view. – M. Deinum Jul 06 '23 at 06:30
  • @M.Deinum, Sir, I did as you suggested, still it is not working as I say after enabling `server.servlet.contextPath=/my-service`. – Deba Jul 06 '23 at 08:19
  • If you put `my-service` as the context path that is the root so `/` is `/my-service/` and not `/` on your server. Everything you write here is for inside your context path. You would need to configure tomcat to redirect always to `/my-service`. – M. Deinum Jul 06 '23 at 08:53
  • Sir, that is exactly I want, by default it should point `localhost:8080/my-service/info`. How can do it Sir if someone simply types `localhost:8080/` – Deba Jul 06 '23 at 09:09

1 Answers1

0

This code may be useful which I got from stackoverflow. This code satisfies my requirement. The link is given below.

Auto-redirect root path to Spring Boot Context Path

import java.io.IOException;
import java.util.Collections;

import org.apache.catalina.Host;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Configuration
public class RootServletConfig {

    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory() {

            @Override
            protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
                super.prepareContext(host, initializers);
                StandardContext child = new StandardContext();
                child.addLifecycleListener(new Tomcat.FixContextListener());
                child.setPath("");
                ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
                child.addServletContainerInitializer(initializer, Collections.emptySet());
                child.setCrossContext(true);
                host.addChild(child);
            }
        };
    }

    private ServletContainerInitializer getServletContextInitializer(String contextPath) {
        return (c, context) -> {
            Servlet servlet = new HttpServlet() {
                @Override
                protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                    resp.sendRedirect(contextPath);
                }
            };
            context.addServlet("root", servlet).addMapping("/");
        };
    }
}
Deba
  • 859
  • 5
  • 9
  • 21