
1. 블로그 글 목록 뷰 구현하기 1-1. 컨트롤러 메서드 1) DTO 구현 - ArticleListViewResponse @Getter public class ArticleListViewResponse { private final Long id; private final String title; private final String content; public ArticleListViewResponse(Article article) { this.id = article.getId(); this.title = article.getTitle(); this.content = article.getContent(); } } 2) BlogViewController - getArticles() 메서드 구현하기 @Re..

템플릿 엔진 : HTML 상에 데이터를넣어 보여주는 도구 다만 HTML과 함께 템플릿 엔진을 위한 문법을 살짝 섞어 사용해야 한다. 템플릿 엔진 개념 잡기 간단한 예시 h1 태그에는 ${이름} 이 text 어트리뷰트로 할당 되어있고, p태그도 비슷하다. 이러한 형태가 템플릿 문법 이렇게 해 두면 서버에서 이름, 나이라는 키로 데이터를 템플릿 엔진에 넘겨주고, 템플릿 엔진은 이를 받아 HTML에 값을 적용한다. 서버에서 보내준 데이터의 예시 - 값이 달라지면 그 때마다 화면에 반영하니 동적인 웹 페이지를 만들 수 있게 되는 것이다. 그림 타임리프 표현식 타임리프 문법 표현식 설명 예제 th:text 텍스트 표현 시 사용 th:text=${person.name} th:each 컬렉션 반복 시 사용 th:ea..

블로그 글 삭제 API 구현하기 1. 서비스 메서드 코드 BlogService @RequiredArgsConstructor // final이 붙거나 @NotNull이 붙은 필드의 생성자 추가 @Service public class BlogService { private final BlogRepository blogRepository; // 게시글을 작성한 유저인지 확인 private static void authorizeArticleAuthor(Article article) { // 현재 사용자 이름을 가져와 게시물의 작성자와 비교하고, 다른 경우에 예외 던짐 String userName = SecurityContextHolder.getContext().getAuthentication().getName(..

글 수정 API 1. 서비스 코드 구현 - update() => 특정 아이디의 글 수정 1) Articles.java - 엔티티에도 update() 메서드 구현 public void update(String title, String content) { this.title = title; this.content = content; } 2) DTO 생성 - request.UpdateArticleRequest @Getter @NoArgsConstructor @AllArgsConstructor public class UpdateArticleRequest { private String title; private String content; } 3) 서비스 코드 구현 - BlogService @RequiredArg..

블로그 글 목록 조회 API 구현하기 1. 서비스 코드 구현 BlogService - findAll() 추가 @RequiredArgsConstructor // final이 붙거나 @NotNull이 붙은 필드의 생성자 추가 @Service public class BlogService { private final BlogRepository blogRepository; // AddArticleRequest를 사용하여 새 게시물을 생성하고, // 지정된 사용자 이름으로 게시물을 저장 public Article save(AddArticleRequest request, String username) { return blogRepository.save(request.toEntity(username)); } //추가 ..

1. 프로젝트 세팅 build.gradle plugins { id 'java' id 'org.springframework.boot' version '3.0.2' id 'io.spring.dependency-management' version '1.1.3' } group = 'com.example' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-s..

식당 비유 - 주문 이후 손님에게 전달될 때까지 상황 생각해보기 : 식당에 가면 손님 -> 점원 -> 주방 이렇게 주문이 전달되고, 나올 때는 주방 -> 점원 -> 손님 순으로 전달된다. 그림으로 정리하면 다음과 같다. API는 클라이언트의 요청을 서버에 잘 전달하고, 서버의 결과물을 클라이언트에게 잘 돌려주는 역할을 한다. # REST API REST REST는 Representational State Transfer 풀어서 말하면 자원을 이름으로 구분해 자원의 상태를 주고받는 API 방식. # REST API 의 특징 / 장단점 1. 특징 - 서버/클라이언트 구조,무상태,캐시 처리 가능,계층화,인터페이스 일관성 2. 장단점 장점 - REST API의 장점은 URL만 보고도 무슨 행동을 하는 API인지..