Although every page on internet says that @RestController is a specification of @Component.I dont know whether it has to be related with DispatcherServlet. But when I try below code by switching between @RestController & @Component, I don't see same behaviour :
First I tried with @RestController:
@RestComponent
public class TestController {
@RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public void testController() {
System.out.println("Hello");
}
}
I got below output in Console:
Hello
Second I tried with @Component + @ResponseBody:
@Component
@ResponseBody
public class TestController {
@RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public void testController() {
System.out.println("Hello");
}
}
I got an error on postman:
{
"timestamp": 1570998345860,
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'POST' not supported",
"path": "/testController"
}
If both annotations are same, then why is there a difference in output ??
Below is the Source code for @RestController & @Controller , which shows that both @RestController & @Controller are specification of @Component:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
}
Maybe it has to be related with DispatcherServlet. It might be possible that Dispatcher Servlet only check for URL in @RestController annotated classes.