部署到tomcat服务器时无法调用spring启动应用程序服务。
我有一个SpringBootApp,我可以在eclipse中运行它,方法是右键固定,然后选择RunAs->Java应用程序。但是,当我构建一个.war文件并将其部署到我的tomcat服务器时。控制台看起来好像应该部署应用程序,但我无法访问我的rest服务。我得到一个404错误。我真的不知道为什么它不工作,所以如果有人知道一个解决方案,你能解释为什么。。。此外,当我查看war文件时,我看到了部署应用程序所需的所有类文件。 你可以下载代码 控制器:部署到tomcat服务器时无法调用spring启动应用程序服务。,spring,rest,tomcat,spring-boot,gradle,Spring,Rest,Tomcat,Spring Boot,Gradle,我有一个SpringBootApp,我可以在eclipse中运行它,方法是右键固定,然后选择RunAs->Java应用程序。但是,当我构建一个.war文件并将其部署到我的tomcat服务器时。控制台看起来好像应该部署应用程序,但我无法访问我的rest服务。我得到一个404错误。我真的不知道为什么它不工作,所以如果有人知道一个解决方案,你能解释为什么。。。此外,当我查看war文件时,我看到了部署应用程序所需的所有类文件。 你可以下载代码 控制器: package com.sample.pkg; i
package com.sample.pkg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@ComponentScan("com.sample.pkg")
public class SampleController {
@Autowired
SampleComponent component;
@RequestMapping("/sendOption")
public String update(@RequestParam(value = "option") String option) {
if (option.equalsIgnoreCase("option1")) {
component.updateStatus(true);
return "Something was updated";
} else if (option.equalsIgnoreCase("option2")) {
component.updateStatus(false);
return "Something else was updated";
}
return "You have entered in an option that is invalid. Please enter a valid option.";
}
@RequestMapping("/getOption")
public String control() {
if (component.getStatus()) {
return "option1";
} else if (!component.getStatus()) {
return "option2";
}
return "error";
}
}
Application.java
package com.chicken.door;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
war {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile("org.springframework.boot:spring-boot-starter-test")
}
输出:
2017-07-18 21:32:13.246 INFO 947 --- [ main] com.sample.pkg.Application : Started Application in 10.6 seconds (JVM running for 10.976)
快速查看项目的代码后,您必须在继续之前修复以下几件事:1)Application.java:删除
@EnableAutoConfiguration
&@PropertySource
2)SampleConfig.java为空?不要忘记使用@Configuration
3)删除/src/main/java/com/sample/pkg/4)SampleEntity.java下的application.properties:使用@EmbeddedId
而不是@Id
5)SampleController.java:删除@ComponentScan
6)不需要额外的SampleComponent.java,直接从控制器使用服务。如何访问控制器。外部tomcat服务器将获取war文件的名称作为上下文根。因此,您的控制器url应该是:{portno}/gs spring boot/getOption