'It seems `@GetMapping` doesn't work with `@Controller`, why is that?

Per the doc, @RestController is just a convenience annotation which combines @Controller and @ResponseBody and @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET), which means @GetMapping should work well with both @RestController and @Controller

In fact @GetMapping only works with @RestController.

@RestController
public class HelloController {
  @GetMapping("/")
  public String hello(){
    return "hello";
  }
}

while

@Controller
public class HelloController {
  @GetMapping("/")
  public String hello(){
    return "hello";
  }
}

doesn't work.

I know I can use @RequestMapping(value = "/", method = RequestMethod.GET) instead, I'd just like to know why. Could someone give a clue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source