我正在尝试以编程方式设置 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 在请求映射之前不附加上下文根?
你为什么要推出自己的解决方案。 Spring-boot 已经支持这一点。
如果您还没有,请将 application.properties
文件添加到 src\main\resources
。在该属性文件中,添加 2 个属性:
server.contextPath=/mainstay
server.port=12378
更新(Spring Boot 2.0)
从 Spring Boot 2.0 开始(由于 Spring MVC 和 Spring WebFlux 的支持),contextPath
已更改为以下内容:
server.servlet.context-path=/mainstay
然后,您可以删除自定义 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,那么您不必通过 Bean 初始化来配置服务器属性。
相反,如果一项功能可用于基本配置,则可以在名为 application
的“属性”文件中进行设置,该文件应位于应用程序结构中的 src\main\resources
下。 “属性”文件有两种格式
.yml .属性
您指定或设置配置的方式因一种格式而异。
在您的特定情况下,如果您决定使用扩展程序 .properties
,那么您将在 src\main\resources
下拥有一个名为 application.properties
的文件,其中包含以下配置设置
server.port = 8080
server.contextPath = /context-path
OTOH,如果您决定使用 .yml
扩展(即 application.yml
),则需要使用以下格式(即 YAML
)设置配置:
server:
port: 8080
contextPath: /context-path
有关 Spring Boot 的更多常见属性,请参阅以下链接:
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
如果您使用 Spring Boot 2.0.0,请使用:
server.servlet.context-path
server.servlet.contextPath=/api
servicename#api
,其中#
被tomcat替换为/
。
请注意,“server.context-path”或“server.servlet.context-path”[从 springboot 2.0.x 开始] 属性仅在您部署到嵌入式容器(例如嵌入式 tomcat)时才有效。例如,如果您将应用程序部署为外部 tomcat 的战争,这些属性将不起作用。
在此处查看此答案:https://stackoverflow.com/a/43856300/4449859
springboot v2.x
和 tomcat v8.5
作为 war
文件部署到外部 tomcat 时,有人知道如何配置它吗?
正确的属性是
server.servlet.path
配置 DispatcherServlet 的路径
和
server.servlet.context-path
在其下方配置应用程序上下文的路径。
您可以通过轻松添加端口和上下文路径来在 [src\main\resources] .properties 文件和 .yml 文件中添加配置
application.porterties 文件配置
server.port = 8084
server.contextPath = /context-path
application.yml 文件配置
server:
port: 8084
contextPath: /context-path
我们也可以在 Spring Boot 中以编程方式更改它。
@Component
public class ServerPortCustomizer implements WebServerFactoryCustomizer<EmbeddedServletContainerCustomizer > {
@Override
public void customize(EmbeddedServletContainerCustomizer factory) {
factory.setContextPath("/context-path");
factory.setPort(8084);
}
}
我们还可以添加其他方式
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {SpringApplication application = new pringApplication(MyApplication.class);
Map<String, Object> map = new HashMap<>();
map.put("server.servlet.context-path", "/context-path");
map.put("server.port", "808");
application.setDefaultProperties(map);
application.run(args);
}
}
使用java命令spring boot 1.X
java -jar my-app.jar --server.contextPath=/spring-boot-app --server.port=8585
使用java命令spring boot 2.X
java -jar my-app.jar --server.servlet.context-path=/spring-boot-app --server.port=8585
我们可以使用属性文件中的一个简单条目来更改上下文根路径。
application.properties
### Spring boot 1.x #########
server.contextPath=/ClientApp
### Spring boot 2.x #########
server.servlet.context-path=/ClientApp
如果您使用的是 spring-boot-starter-webflux ,请使用:
spring:
webflux:
base-path: /api
我向上帝发誓……我每次都忘记这一点。
如果您使用的是 application.yml 和 2.0 以上的 spring 版本,请按以下方式配置。
server:
port: 8081
servlet:
context-path: /demo-api
现在所有的 API 调用都会像 http://localhost:8081/demo-api/
我们可以在 application.properties
中将其设置为 API_CONTEXT_ROOT=/therootpath
我们在Java类中访问它,如下所述
@Value("${API_CONTEXT_ROOT}")
private String contextRoot;
server.contextPath=/mainstay
如果我在 JBOSS 中有一个战争文件,对我有用。在每个包含 jboss-web.xml 的多个战争文件中,它不起作用。我不得不把 jboss-web.xml 放在 WEB-INF 目录中,里面有内容
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<context-root>mainstay</context-root>
</jboss-web>
在 Spring Boot 1.5 中:
在 application.properties
中添加以下属性:
server.context-path=/demo
注意:/demo
是您的上下文路径 URL。
我们可以使用 WebServerFactoryCustomizer
设置它。这可以直接添加到启动 Spring ApplicationContext 的 spring boot 主方法类中。
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
webServerFactoryCustomizer() {
return factory -> factory.setContextPath("/demo");
}
如果你使用 Spring Boot 2.x 并且想在命令行中传递上下文路径属性,你应该像这样输入 double //:
--server.servlet.context-path=//your-path
这对我在 Windows 中运行很有用。
您可以在 Spring Boot: 2.1.6 中使用,如下所示:
server.servlet.context-path=/api-path
对于 Spring boot 2 以下版本,您需要使用以下代码
server:
context-path: abc
对于 Spring boot 2+ 版本,请使用以下代码
server:
servlet:
context-path: abc
您可以将此属性添加到弹簧属性文件
spring.data.rest.basePath=/your_path
<!-- Server port-->
server.port=8080
<!--Context Path of the Application-->
server.servlet.context-path=/ems
它必须是: server.servlet.context-path = / demo 注意它没有引号只有'/'前面的值这个值进入你的application.properties文件
上下文路径可以直接集成到代码中,但不可取,因为它不能重复使用,因此请在 application.properties 文件中写入 server.contextPath=/放置代码的文件夹的名称 contextPath = 放置的文件夹名称代码/注意:仔细观察斜线。
不定期副业成功案例分享
EmbeddedServletContainerCustomizer
的那个也可以工作。但我会去提供所提供的东西,而不是试图靠你自己。为什么您的解决方案不起作用与ServerProperties
中编程的默认行为(意外?)有关,默认配置的contextPath
是""
(它检查null
而不是""
。后者会覆盖您明确设置的contextPath
。