Spring boot 9주차 개발일지

이동규·2023년 7월 17일

Springboot 기초

목록 보기
9/13

REST ful이란

클라이언트와 서버사이가 분리 되어야 한다. Client와 Server 간의 결합성을 줄이기 위한 가이드

1. Client Server Architecture: 서버와 Client 와 분리

2. Statelessness: 상태 저장 x 원하는 기능을 위한 상태는 Client가 가지고 있어야한다. 요청은 서로서로 독립

3. Cacheability: 캐시 가능성 자원의 캐싱이 가능한지의 여부를 항상 표기 해야된다.

4. Layered System: 계층 구조 클라이언트는 서버에 도달 하기까지 과정을 알 필요 없다.

5. Uniform Interface: 일관된 인터페이스

6. Code on Demand(optional): 일시적 기능의 확장 사용 가능한 코드를 응답을 보내 사용자의 기능을 일시적으로 확장 시킬 수있다.

서비스가 커지면 100% Rest의 조건을 만족하기 어렵다.

@RestController // Controller = ResponseBody
@RequestMapping("post")
public class PostRestController {
    private static final Logger logger= LoggerFactory.getLogger(PostRestController.class);
    private final List<PostDto> postlist;// Final: 초기화된 후 변수를 할당 할수 없다.즉 참조값을 변경 하지 않도록 한다.
    public PostRestController() {
        this.postlist = new ArrayList<>();
    }
    //http://localhost:8080/post
    // Post/post
    //Request_body

    @PostMapping()
    @ResponseStatus(HttpStatus.CREATED)// Client 가 더 구체적인 상태를 확인 할수 있다.
    public void createPost(@RequestBody PostDto postdto){
        logger.info(postdto.toString());
        this.postlist.add(postdto);

    }
    //http://localhost:8080/post
    //Get/post

    @GetMapping()
    public List<PostDto> readPostAll(){
        logger.info("Post all");
        return this.postlist;
    }

    //Get/post/0/
    //Get/post?id=0
    @GetMapping("{id}")
    public PostDto readPost(@PathVariable("id") int id){
        logger.info("in read post");
        return this.postlist.get(id);
    }

    //Put/post/0/
    @PutMapping("{id}")
    public void updatePost(@PathVariable("id") int id,@RequestBody PostDto postdto){
        logger.info(id+"is changed");
        PostDto targetPost= this.postlist.get(id);// 기존 리스트에서 업데이트할 인덱스의 값을 targetPost 저장
        if (postdto.getTitle()!= null){
            targetPost.setTitle(postdto.getTitle());// 업데이트를 할 데이터의 제목이 null 값이 아니면 targetPost title 변경
        }
        if (postdto.getContent() != null){
            targetPost.setContent(postdto.getContent());// 업데이트를 할 데이터의 컨텐츠가 null 값이 아니면 targetPost content 변경
        }
        this.postlist.set(id,targetPost);// post list 업데이트
    }

    //Delete/post/0
    @DeleteMapping("{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)// 이하 동무누
    public void deletePost(@PathVariable("id")int id){
        logger.info(id + "is Delete");
        this.postlist.remove(id);
    }
}

1개의 댓글

comment-user-thumbnail
2023년 7월 18일

글이 많은 도움이 되었습니다, 감사합니다.

답글 달기