序文
編集履歴
無し結論
@RestControllerが@Controllerの拡張版。
@ResponceBodyが@Controllerと合併している。
補足
昔は@RestControllerは存在せず、
@ResponceBodyと@Controllerを両方付与する必要があった模様。
HTMLを返す際には@Controllerをまだ用いれるが、
統一して@RestControllerを使用して問題なさそう。
テスト:JSON形式を返す想定(@Controller)
SampleAPI.java
@Controller
public class SampleAPI{
@GetMapping("/")
public Text main() {
return new Text("Hello");
}
}
record Text(String message){};
error.log
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path []: would dispatch back to the current handler URL [/] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
jakarta.servlet.ServletException: Circular view path []: would dispatch back to the current handler URL [/] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
テスト:JSON形式を返す想定(@RestController)
SampleAPI.java
@RestController
public class SampleAPI{
@GetMapping("/")
public Text main() {
return new Text("Hello");
}
}
record Text(String message){};
RestControllerのみJSON形式のデータを返すことができていることが確認できた。
@Controllerの方はビューリゾルバを提供しろとエラーを吐いていることも確認できた。
参考サイト