Java 部署在 Tomcat 中的 Spring Boot 给出 404,但可以独立运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28089129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:34:50  来源:igfitidea点击:

Spring Boot Deployed in Tomcat gives 404 but works Stand-alone

javamavenspring-mvctomcatspring-boot

提问by xelamitchell

I have been testing Spring Boot with embedded Tomcat for about a month now to build a REST API. Everything was working fine. We now want to deploy the API in a separate development environment which has a couple of other (non-Spring) applications running on a Tomcat container.

我已经使用嵌入式 Tomcat 测试 Spring Boot 大约一个月来构建 REST API。一切正常。我们现在想要在单独的开发环境中部署 API,该环境有几个其他(非 Spring)应用程序在 Tomcat 容器上运行。

I made the changes specified in Converting a Spring Boot JAR Application to a WAR using Mavenand Spring Boot Docs.

我进行了使用 MavenSpring Boot Docs将 Spring Boot JAR 应用程序转换为 WAR 中指定的更改。

The deployment goes well (logs are fine, no errors) and looking at Tomcat management i see my application deployed. However, when I attempt to access http://localhost:8080/sophia/usersin curl I get 404.

部署进展顺利(日志正常,没有错误)并且查看 Tomcat 管理我看到我的应用程序已部署。但是,当我尝试在 curl 中访问http://localhost:8080/sophia/users 时,我得到 404。

Any help is much appreciated.

任何帮助深表感谢。

UPDATE:

更新:

Here are my logs:

这是我的日志:

Netbeans:

网豆:

NetBeans: Deploying on Apache Tomcat 8.0.17 profile mode: false debug mode: false force redeploy: true

In-place deployment at /home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT

Deployment is in progress...

deploy?config=file%3A%2Ftmp%2Fcontext1845402702541504208.xml&path=/sophia

OK - Deployed application at context path /sophia

Start is in progress...

start?path=/sophia

OK - Started application at context path /sophia

NetBeans:在 Apache Tomcat 8.0.17 上部署配置文件模式:false 调试模式:false 强制重新部署:true

在 /home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT 就地部署

部署正在进行中...

deploy?config=file%3A%2Ftmp%2Fcontext1845402702541504208.xml&path=/sophia

OK - 在上下文路径 /sophia 部署应用程序

开始进行中...

开始?路径=/索菲亚

OK - 在上下文路径 /sophia 启动应用程序

Tomcat:

雄猫:

INFO 10:47:52:703 org.springframework.boot.context.embedded.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/sophia/*]

INFO 10:47:54:042 org.springframework.boot.SpringApplication - Started application in 8.285 seconds (JVM running for 12087.301)

22-Jan-2015 10:47:54.060 INFO [http-nio-8080-exec-99] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /home/bugz/workspace/server/apache-tomcat-8.0.17/conf/Catalina/localhost/sophia.xml has finished in 12,091 ms

信息 10:47:52:703 org.springframework.boot.context.embedded.ServletRegistrationBean - 映射 servlet:'dispatcherServlet' 到 [/sophia/*]

信息 10:47:54:042 org.springframework.boot.SpringApplication - 在 8.285 秒内启动应用程序(JVM 运行 12087.301)

2015 年 1 月 22 日 10:47:54.060 信息 [http-nio-8080-exec-99] org.apache.catalina.startup.HostConfig.deployDescriptor 部署配置描述符 /home/bugz/workspace/server/apache-tomcat- 8.0.17/conf/Catalina/localhost/sophia.xml 已在 12,091 毫秒内完成

And in sophia.xml for Catalina localhost:

在 Catalina 本地主机的 sophia.xml 中:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" docBase="/home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT" path="/sophia"/>

I've tried accessing

我试过访问

  1. http://localhost:8080/sophia/users
  2. http://localhost:8080/sophia-server-1.0.0-SNAPSHOT/users(name of WAR)
  1. http://localhost:8080/sophia/users
  2. http://localhost:8080/sophia-server-1.0.0-SNAPSHOT/users(WAR 名称)

The first returns a 404 but with the CORS information from my CORS filter bean. The second return 404 without CORS information (which indicates the application has started and is configured but I do not seem to have access to the Controllers).

第一个返回 404 但带有来自我的 CORS 过滤器 bean 的 CORS 信息。第二个返回 404 没有 CORS 信息(这表明应用程序已启动并已配置,但我似乎无法访问控制器)。

采纳答案by M. Deinum

When running an application the path to call consists of a couple of parts.

运行应用程序时,调用路径由几个部分组成。

The first is the base URL on which the application is deployed, in your case that is /sophia.

第一个是部署应用程序的基本 URL,在您的情况下是/sophia.

The second is the servlet mapping of the DispatcherServletin your case that is /sohpia/*.

第二个是DispatcherServlet在您的情况下的 servlet 映射,即/sohpia/*.

The third is the mapping of the controller inside the DispatcherServlet, in your example that is /users.

第三个是控制器内部的映射DispatcherServlet,在你的例子中就是/users.

All those things combined create the URL /sophia/sophia/users.

所有这些东西结合起来创建 URL /sophia/sophia/users

The difference between the deployment as a WAR is that you included a separate URL to deploy on, when running as a jar it, by default, is deployed to /(the root).

作为 WAR 部署之间的区别在于,您包含了一个单独的 URL 来部署,当作为 jar 运行时,默认情况下,它被部署到/(根)。

You could fix it by putting /sophiaas the server.context-pathin the application.propertiesand map the DispatcherServletto /*or /. That will in both situations give you the URL you want (and expected).

您可以通过将/sophiaas放入 并将server.context-pathapplication.properties映射DispatcherServlet/*或来修复它/。这将在两种情况下为您提供所需(和预期)的 URL。

回答by jeet singh parmar

check java -version means the if you complied war in java 8 and tomcat is running on java 7 then it doesn't work.

检查 java -version 意味着如果您在 java 8 中编译了 war 并且 tomcat 在 java 7 上运行,则它不起作用。

回答by Vinoth Kumar Asaithambi

Have you tried extending the SpringBootServletInitializer and overriding the configure method? As stated here

您是否尝试过扩展 SpringBootServletInitializer 并覆盖 configure 方法?如前所述这里

The entry point for jar file is different [Main Class] when compared to running your app as war file inside a container.

jar 文件的入口点与在容器内作为 war 文件运行应用程序相比是不同的 [Main Class]。

Hope this helps.

希望这可以帮助。