我试图以编程方式设置Spring Boot应用程序上下文根 . 上下文根的原因是我们希望从 localhost:port/{app_name}
访问该应用程序并将所有控制器路径附加到它 .
这是web-app的应用程序配置文件 .
@Configuration
public class ApplicationConfiguration {
Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);
@Value("${mainstay.web.port:12378}")
private String port;
@Value("${mainstay.web.context:/mainstay}")
private String context;
private Set<ErrorPage> pageHandlers;
@PostConstruct
private void init(){
pageHandlers = new HashSet<ErrorPage>();
pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));
}
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
logger.info("Setting custom configuration for Mainstay:");
logger.info("Setting port to {}",port);
logger.info("Setting context to {}",context);
factory.setPort(Integer.valueOf(port));
factory.setContextPath(context);
factory.setErrorPages(pageHandlers);
return factory;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
这是主页面的索引控制器 .
@Controller
public class IndexController {
Logger logger = LoggerFactory.getLogger(IndexController.class);
@RequestMapping("/")
public String index(Model model){
logger.info("Setting index page title to Mainstay - Web");
model.addAttribute("title","Mainstay - Web");
return "index";
}
}
应用程序的新根目录应为 localhost:12378/mainstay
,但它仍位于 localhost:12378
.
我错过了什么导致Spring Boot在请求映射之前没有附加上下文根?
9 回答
你为什么要尝试推出自己的解决方案 . Spring-boot已经支持了 .
如果您还没有,请将
application.properties
文件添加到src\main\resources
. 在该属性文件中,添加2个属性:UPDATE (Spring Boot 2.0)
从Spring Boot 2.0开始(由于Spring MVC和Spring WebFlux的支持),
contextPath
已更改为以下内容:然后,您可以删除自定义servlet容器的配置 . 如果需要对容器进行一些后期处理,可以在配置中添加
EmbeddedServletContainerCustomizer
实现(例如添加错误页面) .基本上
application.properties
中的属性用作默认值,您可以使用您提供的工件旁边的另一个application.properties
或通过添加JVM参数(-Dserver.port=6666
)来覆盖它们 .另见The Reference Guide,尤其是properties部分 .
类ServerProperties实现
EmbeddedServletContainerCustomizer
.contextPath
的默认值为""
. 在您的代码示例中,您直接在TomcatEmbeddedServletContainerFactory
上设置contextPath
. 接下来,ServerProperties
实例将处理此实例并将其从您的路径重置为""
. (This line执行null
检查,但默认为""
,它始终失败并将上下文设置为""
,从而覆盖您的上下文) .如果您使用的是Spring Boot,那么您不必通过Vean初始化来配置服务器属性 .
相反,如果一个功能可用于基本配置,则可以在名为
application
的"properties"文件中设置该文件,该文件应位于应用程序结构中的src\main\resources
下 . "properties"文件有两种格式.yml
.properties
指定或设置配置的方式因格式而异 .
在您的特定情况下,如果您决定使用扩展名
.properties
,那么您将在src\main\resources
下使用名为application.properties
的文件,并具有以下配置设置OTOH,如果您决定使用
.yml
扩展名(即application.yml
),则需要使用以下格式设置配置(即YAML
):有关Spring Boot的更常见属性,请参阅以下链接:
如果使用Spring Boot 2.0.0,请使用:
正确的属性是
配置DispatcherServlet的路径
和
在下面配置应用程序上下文的路径 .
请注意,“server.context-path”或“server.servlet.context-path”[从springboot 2.0.x开始]属性仅在部署到嵌入式容器(例如嵌入式tomcat)时才有效 . 例如,如果要将应用程序作为战争部署到外部tomcat,这些属性将不起作用 .
在这里看到这个答案:https://stackoverflow.com/a/43856300/4449859
我们可以在
application.properties
中将其设置为API_CONTEXT_ROOT=/therootpath
我们在Java类中访问它,如下所述
在Spring Boot 1.5中:
在
application.properties
中添加以下属性:注意:
/demo
是您的上下文路径URL .server.contextPath=/mainstay
如果我在JBOSS中有一个war文件,对我有用 . 在多个war文件中,每个war文件都包含jboss-web.xml,但它不起作用 . 我不得不将jboss-web.xml放在带有内容的WEB-INF目录中
上下文路径可以直接集成到代码中,但不建议使用它,因为它无法重用,所以写入application.properties文件server.contextPath = /放置代码的文件夹的名称contextPath =放置文件夹的名称代码/注意:仔细观察斜线 .