【发布时间】:2018-03-07 14:53:34
【问题描述】:
如何在不使用 Spring Boot 的情况下使我的 Spring Rest HelloWorld 应用程序正常工作?
在 Eclipse 中的 tomcat 8.5 中运行此项目时,我希望 url “localhost:8080/hello”显示“HelloWorld”,但它显示的是 404
src/main/java/com.package/HelloController.java
@RestController
public class HelloController {
@RequestMapping("/hello")
public String helloWorld() {
return "Hello World";
}
}
src/main/java/com.package/HelloConfig.java
public class HelloConfig {
@Bean
public HelloController helloController() {
return new HelloController();
}
public static void main(String[] args) {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(HelloConfig.class);
context.getBean(HelloController.class);
}
}
build.gradle
plugins {
id 'java'
id 'war'
id 'eclipse-wtp'
}
dependencies {
compile 'org.springframework:spring-context:5.0.3.RELEASE'
compile 'org.springframework:spring-web:5.0.3.RELEASE'
testCompile 'junit:junit:4.12'
}
repositories {
mavenCentral()
}
【问题讨论】:
-
其余配置如何?您如何部署应用程序?
-
您是否将控制器配置为 bean 或设置组件扫描?
-
您可能在 url 中有一些额外的上下文。试试
localhost:8080/<name of your war file>/hello