我正在使用 Spring @RestController 构建 Web 服务。本地测试时,一切正常。但是,将应用程序部署到远程应用程序服务器后,当我访问同一端点时,我会收到 404 错误。这是我的代码:
@RestController
public class HelloRestController
{
@RequestMapping( value = "/hello", method = RequestMethod.GET )
public String hello()
{
return "hello";
}
}
这是我的 Web 应用程序初始化程序。注意:我没有 web.xml 文件。
public class MyApplicationInitializer implements WebApplicationInitializer
{
@Override
public void onStartup( ServletContext servletContext ) throws ServletException
{
AnnotationConfigWebApplicationContext ctx =
new AnnotationConfigWebApplicationContext();
servletContext.addListener( new ContextLoaderListener( ctx ) );
ctx.register( WebApplicationConfig.class, ApplicationConfig.class );
ctx.setServletContext( servletContext );
Dynamic dispatcher =
servletContext.addServlet( "SpringServlet", new DispatcherServlet( ctx ) );
dispatcher.addMapping( "/" );
dispatcher.setLoadOnStartup( 1 );
}
}
当我将应用程序本地部署到 Tomcat-8.0.15 并点击“localhost:8080/myApp/hello”时,它返回“hello”。但是,将应用程序部署到远程应用程序服务器(也是 Tomcat 8)后,当我尝试访问同一端点时,我收到 404。
预先感谢您的帮助!
最佳答案
代码看起来不错。仔细检查是否有任何与环境相关的问题会导致 spring 的 bean 初始化失败——这将导致调度程序 servlet 失败,从而导致 404。常见问题可能是数据库连接被拒绝、类路径冲突、JVM 之间的差异远程应用程序服务器和本地。
您应该能够找到一些 tomcat 日志来查明远程服务器是否存在任何问题。
关于java - 部署到应用程序服务器后出现 404,但不在本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28618957/