SpringBoot : @RequestMappingと@GetMappingの違い
### 目的
@RequestMapping
や、@GetMapping
が記述されているけど違いがわからない
結論
@RequestMapping
GET, POST, PUT, DELETE をするURI全ての前段階のMappingを記述する
@GetMapping
例
/** * コントローラクラスであることの宣言 */ @RestController /** * リクエストURLに対しての処理メソッド定義 */ @RequestMapping("/messages") public class MessageController { /** * 他クラス呼び出し */ @Autowired private MessageService messageService; /** * RequestmappingのGet拡張(全件取得) */ @GetMapping public Collection<Message> getMessages() { return messageService.getMessages(); } /** * RequestmappingのGet拡張(idごとの取得) * * @throws NotFoundException */ @GetMapping("/{id}") public Optional<Message> getMessageById(@PathVariable String id) throws NotFoundException { return messageService.getMessageById(id); } /** * RequestmappingのPost拡張(登録) */ @PostMapping public Message postMessage(@RequestBody @Validated Message message) { return messageService.createMessage(message); } /** * RequestmappingのDelete拡張(idごとの削除) * * @throws NotFoundException */ @DeleteMapping("/{id}") public Optional<Message> deleteMessageById(@PathVariable String id) throws NotFoundException { return messageService.deleteMessageById(id); } /** * RequestmappingのPut拡張(idごとの更新) * * @throws NotFoundException */ @PutMapping("/{id}") public Optional<Message> updateMessageById(@PathVariable String id, @RequestBody MessageUpdatePayload messageUpdatePayload) throws NotFoundException { return messageService.updateMessageById(id, messageUpdatePayload); } }