【问题标题】:Tomcat using war name as base url for Spring bootTomcat 使用战争名称作为 Spring 启动的基本 url
【发布时间】: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


【解决方案1】:

您还可以将 Tomcat 服务器上的应用名称更改为:

  1. 在“服务器”视图中双击您的服务器实例
  2. 将选项卡从“概览”切换到“模块”(位于配置屏幕底部)
  3. 在列表中选择您的应用程序,然后单击右侧的“编辑”
  4. 将“路径”设置为“springapp”(或任何您想要的 URI 名称)
  5. 点击“确定”并保存更改。

现在您的应用将位于 URI:http://localhost:8080/springapp

【讨论】:

  • 感谢您的解决方案。但我应该在应用程序本身中提供所有配置。
  • 不客气。我在根级别“/”(无项目名称)使用我的项目,并且通过此设置,我非常有资格管理它。
【解决方案2】:

你应该可以像这样重命名战争

<build>
  <finalName>springapp</finalName>
 . . .
</build>

【讨论】:

  • 1.您无需重命名 war 即可更改 tomcat 中的上下文路径。 2.这是Maven POM的一部分,OP的问题中没有暗示他正在使用Maven进行构建
  • 谢谢:)。我正在使用 maven,但我不应该更改 finalName 。
【解决方案3】:

如果您使用的是 Eclipse,那么您可以通过转到 yourproject-&gt;properties-&gt;Web Project settings 来更改根上下文。然后在此处更改上下文根目录并重新启动服务器

【讨论】:

  • 谢谢,这是一种方法。但我仅将 eclipse 用于测试目的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 2016-04-25
  • 2012-04-24
  • 2021-10-16
  • 2019-09-30
  • 2019-10-27
  • 2015-05-09
相关资源
最近更新 更多