我整理了一个简单的Eclipse Spring Boot Rest程序 . 只是几个返回一些字符串的 endpoints . 我正在使用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'
}
1 回答
我已经回答了类似的问题here .
您正在使用Java 1.7 JRE运行外部tomcat,同时已针对1.8编译代码 .
奇怪的是,没有错误,应用程序出现在经理应用程序中,但是当您尝试访问它时,您会得到404 .
确认这一点的一种方法是查看您的tomcat日志输出,您将看不到Spring Boot Banner .
要解决此问题,您可以针对Java 1.7编译代码 .