이제 HTTP GET 동사를 사용하여 엔드포인트를 만드는 방법을 이해했다. 계속해서 License 클래스 인스턴스의 생성, 수정, 삭제를 위해 POST, PUT, DELETE 메서드를 추가해 보자. 코드 3-5에서 이것을 수행할 방법을 보여 준다.
코드 3-5 HTTP 엔드포인트 개별 노출하기
package com.optimagrowth.license.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.optimagrowth.license.model.License;
import com.optimagrowth.license.service.LicenseService;
@RestController
@RequestMapping(value="v1/organization/{organizationId}/license")
public class LicenseController {
@Autowired
private LicenseService licenseService;
@RequestMapping(value="/{licenseId}", method=RequestMethod.GET)
public ResponseEntity<License> getLicense(
@PathVariable("organizationId") String organizationId,
@PathVariable("licenseId") String licenseId) {
License license = licenseService.getLicense(licenseId, organizationId);
return ResponseEntity.ok(license);
}
@PutMapping ➊
public ResponseEntity<String> updateLicense(
@PathVariable("organizationId")
String organizationId,
@RequestBody License request) { ➋
return ResponseEntity.ok(licenseService.updateLicense(request, organizationId));
}
@PostMapping ➌
public ResponseEntity<String> createLicense(
@PathVariable("organizationId") String organizationId,
@RequestBody License request) {
return ResponseEntity.ok(licenseService.createLicense(request, organizationId));
}
@DeleteMapping(value="/{licenseId}") ➍
public ResponseEntity<String> deleteLicense(
@PathVariable("organizationId") String organizationId,
@PathVariable("licenseId") String licenseId) {
return ResponseEntity.ok(licenseService.deleteLicense
(licenseId, organizationId));
}
}
➊ 라이선스를 업데이트하는 PUT 메서드
➋ HTTP 요청 바디(내용)를 라이선스 객체로 매핑
➌ 라이선스를 생성하는 POST 메서드
➍ 라이선스를 삭제하는 DELETE 메서드