【发布时间】:2015-09-04 21:40:08
【问题描述】:
我整理了一个简单的 Eclipse Spring Boot Rest 程序。只是几个返回一些字符串的端点。我正在使用 Eclipse Gradle 插件构建它。没问题,它在 Eclipse 提供的 Tomcat 实例中运行良好。我还在本机 Windows Tomcat 上运行它。我使用部署功能使用 tomcat Web 应用程序管理器部署了 .war 文件。运行良好。然后我部署了一个 ec2 ubuntu 实例并再次使用 tomcat Web 应用程序管理器并上传 .war 文件。其余程序正确显示在应用程序窗口中,但不会运行。我使用了以下网址,
http://ec2-IP-Address/demo-0.0.1-SNAPSHOT/hello
我不断收到 404。我很确定 tomcat 部署和 ec2 环境看起来是正确的,因为我可以在 ec2 实例中运行 tomcat7-examples 而没有问题。任何帮助将不胜感激。
AwsElbRestServerAApplication.java 文件:
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class AwsElbRestServerAApplication {
public static void main(String[] args) {
SpringApplication.run(AwsElbRestServerAApplication.class, args);
}
}
ServletInitializer.java 文件:
package demo
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AwsElbRestServerAApplication.class);
}
}
RestController.java 文件:
package demo
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RestController {
@RequestMapping("/hello")
public @ResponseBody String hello() {
return "Greetings from Server A";
}
@RequestMapping("/heartbeat")
public @ResponseBody String heartbeat() {
return "Server A is still running";
}
}
build.gradle 文件:
buildscript {
ext {
springBootVersion = '1.2.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle
plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management
Plugin:0.5.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
war {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
jcenter()
maven { url "http://repo.spring.io/libs-snapshot" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-remote-shell")
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/
org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/
JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
【问题讨论】:
-
我昨天才回答过类似的问题stackoverflow.com/questions/30908579/… 有没有可能是同一个问题?
-
是的,你回答了。我刚刚将我的 Eclipse jre 更改为 1.7 并且它起作用了!默认情况下,Eclipse 在项目创建菜单中选择 Java 1.8,但我使用的是 Tomcat7。非常感谢,很棒的收获!
-
嘿 ci_ 你能用一个答案来回答你的问题吗?这样我就可以给你正确的答案了?
-
我已经发布了答案。
标签: tomcat amazon-ec2 spring-boot war