java - Spring应用程序对第​​三方应用程序上的所有URL请求(除了/)返回404(在本地主机上工作正常)

标签 java spring http-status-code-404 web.xml

在我的 Spring 应用程序中,我有以下内容:

@RequestMapping(method = RequestMethod.GET, value = "/")
public String goHome(ModelMap map){
    map.addAttribute("content", "home.jsp");
    return "index";
}


@RequestMapping(method = RequestMethod.GET, value = "register")
public String getRegisterPage(ModelMap map){
    map.addAttribute("content", "register.jsp");
    return "index";
}

在本地主机上,我可以分别以 localhost:8080localhost:8080/register 身份访问它们。但是,当我将它们部署在第三方服务器上时,domain.com 工作正常,但 domain.com/register(以及除 domain.com 之外的所有其他链接)会导致 404。

Not Found

The requested URL /register was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

注意:
- localhost 和domain.com 都运行在Tomcat 5.5 上并且具有Servlet 版本2.4
- 我什至将 RequestMapping 值更改为 value="/register" 但没有任何变化

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Adsense</display-name>
  <listener>
    <listener-class>com.adsense.connection.MySqlDBPooling</listener-class>
  </listener>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

感谢帮助

最佳答案

在这里得到我的答案:Tomcat 404 Not Found error

The problem is that in the production environment tomcat is being fronted by an apache httpd which does not forward requests to tomcat for urls of the form /foobar (without an extension). Apache doesn't know how to deal with those URLs and tries to serve them as static files from the disk. The files are not there so it tries to execute the 404 rule which is badly configured (the configured 404 file is itself missing as well - but that's another problem).

Solutions:

Assign an extension to all your servlets and specify that to apache. E.g .do (like struts does) so your servlet becomes /foobar.do. You'll then need to ask your sysadmin to specify that *.do goes to tomcat. Ask your sysadmin to configure apache to send all requests to tomcat apart of those for static files. E.g *.jpg, *.png, *.css, *.js etc

就我而言,它可以很好地处理像/register.do 这样的 url:)

关于java - Spring应用程序对第​​三方应用程序上的所有URL请求(除了/)返回404(在本地主机上工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8093876/

相关文章:

java - 外键错误

java - 如何使用 Spring Security 5 在 Spring Boot 应用程序(不是 Web 应用程序)中获取 oauth2 访问 token

python - 为什么我在 Django 中的测试函数在 chrome 浏览器中正确显示页面时返回状态代码 404?

Java - 如何获取交错迭代器/集合

Java 设置图像资源

java - BindingResult 和 bean 名称的普通目标对象都不能用作请求属性 - Spring MVC

spring - 如何在 i18n Thymeleaf/Spring 消息的一部分中定义链接?

c# - WCF 服务应用程序使用 WebGet 返回 404

python - 设置 URL 时 Django 'Page not found' 错误

java - 从 Eclipse 推送到 Openshift : Read timeout after 30, 000 毫秒