【问题标题】:AWS EC2, Tomcat7, Spring Boot, web, mvc, REST - http get method is not workingAWS EC2、Tomcat7、Spring Boot、web、mvc、REST - http get 方法不起作用
【发布时间】:2016-02-06 11:14:57
【问题描述】:

我使用 Spring Boot 开发了“hello-world”Web 应用程序,并且在本地(在嵌入式 Tomcat 上)它运行良好。

但是当我将 WAR 文件上传到安装在 AWS EC2 上的 Tomcat7 时,我在调用简单的 http get 方法时遇到了问题 - 我得到了 GET http://my_server_address:8080/ping 404(未找到)。

所有静态资源都可以正常工作(html、css 文件)。

我的控制器看起来像:

@RestController
public class PingController {
@RequestMapping(value = "/ping", method = RequestMethod.GET)
public String ping() {
    return "Live!";
}
}

我的主要课程如下所示:

@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ServletInitializer.class);
}

public static void main(String[] args) {
    SpringApplication.run(ServletInitializer.class, args);
}
}

我认为 DispatcherServlet 有问题?我没有 web.xml - 只有简单的 application.properties

有人可以帮助我吗? 提前致谢!

【问题讨论】:

    标签: java spring-mvc tomcat amazon-ec2 spring-boot


    【解决方案1】:

    是的,您对DispatcherServlet 有疑问是对的。但更准确地说,您对 Spring Context 有疑问。

    当应用程序通过 Spring Boot 运行时,Spring Boot 关心 Spring Context 和组件扫描。

    如果您想使用 Tomcat 运行 Spring MVC 应用程序,您必须通过 XML 或 Java Base 配置手动配置 Spring Application Context。如果您不想使用web.xml,您可以使用带有WebApplicationInitializer 的“完整Java Base”配置(您使用的是Tomcat 7,因此它支持Servlet API 3.0)

    顺便说一句,我建议您创建web.xml 以在Java Web Server 中运行Java Web 应用程序,如Tomcat。

    请在Spring MVC documentation 中寻找更多信息。

    【讨论】:

    • 非常感谢您的回答!我已经意识到我不需要安装 Tomcat - 将我的应用程序作为 Spring Boot 应用程序运行就足够了。所以我将包装更改为“jar”并使用“java -jar myjar.jar”运行:)
    猜你喜欢
    • 2021-10-05
    • 2021-03-05
    • 2018-06-20
    • 2019-05-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    相关资源
    最近更新 更多