【发布时间】:2017-01-17 05:44:06
【问题描述】:
我有一个名为“springapp”的 Spring Boot 应用程序,它的基本 URI 为:
http://localhost:8080/
当我将它作为独立应用程序运行时,它运行良好。但是当我在 Eclipse 中像“run as --> Run on Server”一样运行它时,它会将战争名称附加为 springapp-1.0-snapshot 并且基本 URL 将变为:
http://localhost:8080/springapp-1.0-snapshot
有什么办法可以解决这个问题。我希望 URI 是
http://localhost:8080/springapp
我在 application.properties 中尝试了以下所有配置,但都没有奏效:
spring.webservices.path=/springapp
server.tomcat.basedir=/springapp
spring.jersey.application-path=/springapp
server.contextPath=/springapp
server.servlet-path=/springapp
这是我的入门课程:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@ImportResource("classpath:spring-config.xml")
public class SpringAppStarter extends SpringBootServletInitializer {
private static final Logger GENERAL_LOG = LogManager.getLogger(SpringAppStarter .class);
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(SpringAppStarter.class);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GENERAL_LOG.info("Starting Spring Service App");
SpringApplication.run(SpringAppStarter .class, args);
}
}
还有我的请求映射类:
@RestController
@RequestMapping("/Customers")
public class SpringAppResource {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
我还在 META-INF 文件夹中添加了上下文配置:
<Context>
<context docBase="springapp" path="/springapp" reloadable="true"/>
</Context>
我正在使用 Maven 来构建项目。而且我不想更改服务名称。我需要在应用程序本身的某个地方提供上下文路径。不能对服务器有任何依赖。我应该提供所有配置,所以部署后不需要进行任何配置。
【问题讨论】:
-
Tomcat 默认使用你的war文件名作为上下文路径。快速搜索给了我这个:stackoverflow.com/q/7276989/395202 告诉您如何用作应用程序的上下文路径,或 tomcat.apache.org/tomcat-5.5-doc/config/context.html 了解如何在 Tomcat 中设置上下文路径
-
谢谢。理想情况下,这就是我们为常规 Web 服务所做的事情。我已经尝试过了,但它不适用于 spring boot。
-
我认为这里与 Spring Boot 无关。上下文路径应该是由容器决定的,在 web 应用程序(这里使用 Spring Boot)可以做任何事情之前。
-
啊...我刚刚发现您正在通过 Eclipse 运行它。 iirc,在运行 Tomcat 的 Eclipse 中有一些技巧来提供 Tomcat 配置。类似于有一个包含配置的单独目录,以便 Eclipse 每次都会复制到 Tomcat 目录。因此,如果您直接更新 Tomcat 目录中的配置,它将无法正常工作,因为它会被静默替换。
-
完全正确。它适用于普通的 Spring Web 服务。我只是无法为弹簧靴做这件事。
标签: java spring tomcat spring-boot