「Spring」@RestControllerと@PathVariableでDELETE APIを実装する

2021年9月3日

書式
public ResponseEntity<Object> delete(@PathVariable(変数名) String 変数名)

使用例

@RestController
public class ProductServiceController {
   //HashMap型pdRepoの宣言
   private static Map<String, Product> pdRepo = new HashMap<>();
   
   //リクエストURI /products/{id}
   @RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)
   public ResponseEntity<Object> delete(@PathVariable("id") String id) { 
      // @PathVariableでパス変数{id}を取得
      pdRepo.remove(id);
      return new ResponseEntity<>("製品が削除されました", HttpStatus.OK);
   }
}

 

Spring Boot

Posted by arkgame