【发布时间】:2015-01-18 13:24:01
【问题描述】:
所以有一个来自 Spring 网站的示例:
package com.example;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
效果很好。有注释@RestController是@Controller+@ResponseBody和@SpringBootApplication注解等价于使用@Configuration、@EnableAutoConfiguration和@ComponentScan。
我的问题是:为什么会出现 404 错误如果我使用下面的注释访问“/”?
@ResponseBody
@SpringBootApplication
public class Example { ... }
【问题讨论】:
-
不,
@SpringBootApplication注释不等同于此。它旨在用于测试以表明测试用例也需要引导 Spring Boot.. -
@SpringBootApplication注释是 1.2 中的新注释,它等效于@Configuration、@EnableAutoConfiguration和@ComponentScan。要指示测试应该引导引导,请使用@IntegrationTest -
@M.Deinum:该文本是从这里复制的:docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/…
-
你说得对,我把
@SpringApplicationConfiguration和这个混淆了。@SpringBootApplication是一个新添加的内容,它确实应该像您所说的那样工作。尽管(如答案所述)您缺少@RestController或@Controller注释。
标签: spring annotations http-status-code-404 spring-boot