在Tomcat中部署的Spring Boot提供了404但是可以独立运行 [英] Spring Boot Deployed in Tomcat gives 404 but works Stand-alone

查看:402
本文介绍了在Tomcat中部署的Spring Boot提供了404但是可以独立运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我指定了更改在使用Maven将Spring Boot JAR应用程序转换为WAR Spring Boot Docs

部署顺利(日志很好,没有错误),看着Tomcat管理,我看到我的应用程序已部署。但是,当我尝试以curl方式访问 http:// localhost:8080 / sophia / users 时,我得到404.

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/users in curl I get 404.

非常感谢任何帮助。

更新:

这里是我的日志:

Netbeans:


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

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

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

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

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

确定 - 在上下文路径/ sophia部署的应用程序

OK - Deployed application at context path /sophia

开始正在进行中......

Start is in progress...

start?path = / sophia

start?path=/sophia

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

OK - Started application at context path /sophia

Tomcat:


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

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 - 在8.285秒内启动应用程序(JVM运行12087.301)

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

2015年1月22日10:47:54.060 INFO [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 ms

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

在Catalina localhost的sophia.xml中:

And in sophia.xml for Catalina localhost:

<?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"/>

我试过访问


  1. http:// localhost:8080 / sophia / users

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

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

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

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).

推荐答案

运行应用程序时,调用路径包含几个部分。

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

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

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

第二个是你的案例中 DispatcherServlet 的servlet映射 / sohpia / *

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

第三个是 DispatcherServlet 内控制器的映射,您的示例 / users

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

所有这些组合创建的URL / sophia /蒿/用户

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

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

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).

您可以通过将 / sophia 作为 server.context-path <来修复它/ code>在 application.properties 中,并将 DispatcherServlet 映射到 / * / 。这将在两种情况下为您提供您想要(和预期)的URL。

You could fix it by putting /sophia as the server.context-path in the application.properties and map the DispatcherServlet to /* or /. That will in both situations give you the URL you want (and expected).

这篇关于在Tomcat中部署的Spring Boot提供了404但是可以独立运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆