How to redirect to a controller or a web page from localhost:8080/ in spring boot 3.1.1 with server.servlet.contextPath

huangapple go评论79阅读模式
英文:

How to redirect to a controller or a web page from localhost:8080/ in spring boot 3.1.1 with server.servlet.contextPath

问题

我有一个要求,即在localhost:8080的情况下,它应该指向Spring Boot应用程序中的默认网页。我目前正在使用Spring 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状态404 - 未找到的错误。

请给我提供建议。我想要重定向到一个带有contextPath的网页。如果有人访问localhost:8080,它应该重定向到localhost:8080/my-service/info

请帮助我。

英文:

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 = &quot;/&quot;)
	public ResponseEntity&lt;String&gt; getValue() {
		return ResponseEntity.ok(&quot;Hello World&quot;);
	}

    @GetMapping(path = &quot;/info&quot;)
	public ResponseEntity&lt;String&gt; getInfoValue() {
		return ResponseEntity.ok(&quot;Some Information&quot;);
	}
}

I have also tried with the following @Configuration option.

@Configuration
public class WebMVCApplicationConfig implements WebMvcConfigurer {

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

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.

答案1

得分: 0

这段代码可能对你有用,我从stackoverflow上找到的。这段代码满足了我的需求。以下是链接:

https://stackoverflow.com/questions/56188883/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("/");
        };
    }
}
英文:

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

https://stackoverflow.com/questions/56188883/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(&quot;&quot;);
ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
child.addServletContainerInitializer(initializer, Collections.emptySet());
child.setCrossContext(true);
host.addChild(child);
}
};
}
private ServletContainerInitializer getServletContextInitializer(String contextPath) {
return (c, context) -&gt; {
Servlet servlet = new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect(contextPath);
}
};
context.addServlet(&quot;root&quot;, servlet).addMapping(&quot;/&quot;);
};
}
}

huangapple
  • 本文由 发表于 2023年7月6日 14:19:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定