REST ful이란
클라이언트와 서버사이가 분리 되어야 한다. Client와 Server 간의 결합성을 줄이기 위한 가이드
1. Client Server Architecture: 서버와 Client 와 분리
2. Statelessness: 상태 저장 x 원하는 기능을 위한 상태는 Client가 가지고 있어야한다. 요청은 서로서로 독립
3. Cacheability: 캐시 가능성 자원의 캐싱이 가능한지의 여부를 항상 표기 해야된다.
4. Layered System: 계층 구조 클라이언트는 서버에 도달 하기까지 과정을 알 필요 없다.
6. Code on Demand(optional): 일시적 기능의 확장 사용 가능한 코드를 응답을 보내 사용자의 기능을 일시적으로 확장 시킬 수있다.
서비스가 커지면 100% Rest의 조건을 만족하기 어렵다.
@RestController
@RequestMapping("post")
public class PostRestController {
private static final Logger logger= LoggerFactory.getLogger(PostRestController.class);
private final List<PostDto> postlist;
public PostRestController() {
this.postlist = new ArrayList<>();
}
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
public void createPost(@RequestBody PostDto postdto){
logger.info(postdto.toString());
this.postlist.add(postdto);
}
@GetMapping()
public List<PostDto> readPostAll(){
logger.info("Post all");
return this.postlist;
}
@GetMapping("{id}")
public PostDto readPost(@PathVariable("id") int id){
logger.info("in read post");
return this.postlist.get(id);
}
@PutMapping("{id}")
public void updatePost(@PathVariable("id") int id,@RequestBody PostDto postdto){
logger.info(id+"is changed");
PostDto targetPost= this.postlist.get(id);
if (postdto.getTitle()!= null){
targetPost.setTitle(postdto.getTitle());
}
if (postdto.getContent() != null){
targetPost.setContent(postdto.getContent());
}
this.postlist.set(id,targetPost);
}
@DeleteMapping("{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deletePost(@PathVariable("id")int id){
logger.info(id + "is Delete");
this.postlist.remove(id);
}
}
글이 많은 도움이 되었습니다, 감사합니다.