Spring MVC 如何使用server.servlet.contextPath从spring Boot 3.1.1中的localhost:8080/重定向到控制器或网页

92dk7w1h  于 2023-08-06  发布在  Spring
关注(0)|答案(1)|浏览(92)

我有一个要求,在localhost:8080的情况下,它应该指向spring Boot 应用程序中的默认网页。我现在使用的是Sping Boot 3.1.1。正常情况下一切正常,但我添加server.servlet.contextPath=my-service,它不工作。我提供下面的代码。
我的控制器代码

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

字符串
我还尝试了以下@Configuration选项。

@Configuration
public class WebMVCApplicationConfig implements WebMvcConfigurer {

    @Override
   public void addViewControllers(ViewControllerRegistry registry) {
       registry.addRedirectViewController("/", "/my-service/info");
   }
}


如果我在application.properties中注解以下内容,则一切正常。

# server.servlet.contextPath=/my-service


如果我取消注解contextPath,我会得到HTTP Status 404 - Not Found
请建议我。我想重定向到沿着contextPath的网页。如果有人正在访问localhost:8080,它应该重定向到localhost:8080/my-service/info
请帮帮我

rfbsl7qr

rfbsl7qr1#

这段代码可能是有用的,我从stackoverflow得到的。这段代码满足了我的要求。链接在下面。
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("/");
        };
    }
}

字符串

相关问题