So there is an example from the Spring web-site:
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);
}
}
which works fine. There are notes @RestController
is @Controller
+ @ResponseBody
and The @SpringBootApplication
annotation is equivalent to using @Configuration
, @EnableAutoConfiguration
and @ComponentScan
.
My question is: Why is there 404 error accessing "/" if I use annotations below?
@ResponseBody
@SpringBootApplication
public class Example { ... }
Using the @ResponseBody
annotation alone isn't enough to tell Spring that your class is a MVC controller. You either need to use @RestController
:
@RestController
@SpringBootApplication
public class Example { ... }
or @Controller
and @ResponseBody
:
@Controller
@ResponseBody
@SpringBootApplication
public class Example { ... }