Spring boot

블로그 5 - 글 목록 조회 API 만들기

ryeonng 2024. 10. 2. 15:28
BlogService 파일에 게시글 전체 조회 기능 추가 - 1
package com.tenco.demo._domain.blog.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tenco.demo._domain.blog.dto.ArticleDTO;
import com.tenco.demo._domain.blog.entity.Article;
import com.tenco.demo._domain.blog.repository.PostRepository;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service // IoC (빈으로 등록)
public class BlogService {
	
	@Autowired // DI
	private final PostRepository postRepository;
	
	@Transactional // 쓰기 지연 처리 까지
	public Article save(ArticleDTO dto) {
		// 비즈니스 로직이 필요하다면 작성
		return postRepository.save(dto.toEntity());
	}
	
	// 전체 게시글 조회 기능
	public List<Article> findAll() {
		List<Article> articles = postRepository.findAll();
		return articles;
	}
}

 

ApiUtil - 2
package com.tenco.demo.common;

public class ApiUtil<T> {

	private Integer status; // 응답 상태 코드 저장 (200, 400, 500)
	private String msg; // 응답 메시지 저장 ("서버연산 에러입니다." "잘못된 접근입니다.")
	private T data; // 응답의 실제 데이터 저장 (제네릭 활용)
	
	// 성공적인 응답을 반환할 때 사용하는 생성자
	public ApiUtil(T body) {
		this.status = 200; // 통신 성공
		this.msg = "성공";
		this.data = body;
	}
	
	// 커스텀 상태코드와 메시지를 반환 시킬 때 사용하는 생성자 (예 : 에러 응답)
	public ApiUtil(Integer status, String msg) {
		this.status = status;
		this.msg = msg;
		this.data = null;
	}
}

 

BlogController 코드 추가 - 3
package com.tenco.demo._domain.blog.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.tenco.demo._domain.blog.dto.ArticleDTO;
import com.tenco.demo._domain.blog.entity.Article;
import com.tenco.demo._domain.blog.service.BlogService;
import com.tenco.demo.common.ApiUtil;

import lombok.RequiredArgsConstructor;


@RequiredArgsConstructor
@RestController // @Controller + @ResponseBody
public class BlogApiController {
	
	private final BlogService blogService;
	
	// URL 매핑 : 주소설계 - http://localhost:8080/api/articles
	@PostMapping("/api/articles")
	public ResponseEntity<Article> addArticle(@RequestBody ArticleDTO dto) {
		// 1. 인증 검사
		// 2. 유효성 검사
		Article savedArticle = blogService.save(dto);
		return ResponseEntity.status(HttpStatus.CREATED).body(savedArticle);
	}
	
	@GetMapping("/api/articles")
	public ApiUtil<List<Article>> getAllArticles() {
		List<Article> articles = blogService.findAll();
		if(articles.isEmpty()) {
			return new ApiUtil<>(204, "조회된 게시글이 없습니다.");
		}
		return new ApiUtil<>(articles);
	}
	
}

 

ExceptionHandler 만들기 - 4

 

 

사용자 정의 예외 클래스 만들기
package com.tenco.demo.common.errors;

public class Exception400 extends RuntimeException{

	public Exception400(String msg) {
		super(msg);
	}
	
}

 

ExceptionHandler 만들기
package com.tenco.demo.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.tenco.demo.common.errors.Exception400;
import com.tenco.demo.common.errors.Exception401;
import com.tenco.demo.common.errors.Exception403;
import com.tenco.demo.common.errors.Exception404;
import com.tenco.demo.common.errors.Exception500;

// RuntimeException 터지면 해당 파일로 오류가 모인다. 
@RestControllerAdvice // C.A --> 뷰 에러페이, R.C.A 데이터 반환 (에러)
public class MyExceptionHandler {

	private static final Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);

	@ExceptionHandler(Exception400.class)
	public ResponseEntity<ApiUtil<Object>> ex400(Exception400 e) {
		logger.error("400 Error: {}", e.getMessage());
		ApiUtil<Object> apiUtil = new ApiUtil<>(400, e.getMessage());
		return new ResponseEntity<>(apiUtil, HttpStatus.BAD_REQUEST);
	}

	@ExceptionHandler(Exception401.class)
	public ResponseEntity<ApiUtil<Object>> ex401(Exception401 e) {
		logger.error("401 Error: {}", e.getMessage());
		ApiUtil<Object> apiUtil = new ApiUtil<>(401, e.getMessage());
		return new ResponseEntity<>(apiUtil, HttpStatus.UNAUTHORIZED);
	}

	@ExceptionHandler(Exception403.class)
	public ResponseEntity<ApiUtil<Object>> ex403(Exception403 e) {
		logger.error("403 Error: {}", e.getMessage());
		ApiUtil<Object> apiUtil = new ApiUtil<>(403, e.getMessage());
		return new ResponseEntity<>(apiUtil, HttpStatus.FORBIDDEN);
	}

	@ExceptionHandler(Exception404.class)
	public ResponseEntity<ApiUtil<Object>> ex404(Exception404 e) {
		logger.error("404 Error: {}", e.getMessage());
		ApiUtil<Object> apiUtil = new ApiUtil<>(404, e.getMessage());
		return new ResponseEntity<>(apiUtil, HttpStatus.NOT_FOUND);
	}

	@ExceptionHandler(Exception500.class)
	public ResponseEntity<ApiUtil<Object>> ex500(Exception500 e) {
		logger.error("500 Error: {}", e.getMessage());
		ApiUtil<Object> apiUtil = new ApiUtil<>(500, e.getMessage());
		return new ResponseEntity<>(apiUtil, HttpStatus.INTERNAL_SERVER_ERROR);
	}

}

 

Controller 코드 수정
package com.tenco.demo._domain.blog.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.tenco.demo._domain.blog.dto.ArticleDTO;
import com.tenco.demo._domain.blog.entity.Article;
import com.tenco.demo._domain.blog.service.BlogService;
import com.tenco.demo.common.ApiUtil;
import com.tenco.demo.common.errors.Exception400;

import lombok.RequiredArgsConstructor;


@RequiredArgsConstructor
@RestController // @Controller + @ResponseBody
public class BlogApiController {
	
	private final BlogService blogService;
	
	// URL 매핑 : 주소설계 - http://localhost:8080/api/articles
	@PostMapping("/api/articles")
	public ResponseEntity<Article> addArticle(@RequestBody ArticleDTO dto) {
		// 1. 인증 검사
		// 2. 유효성 검사
		Article savedArticle = blogService.save(dto);
		return ResponseEntity.status(HttpStatus.CREATED).body(savedArticle);
	}
	
	@GetMapping(value = "/api/articles", produces = MediaType.APPLICATION_JSON_VALUE)
	public ApiUtil<?> getAllArticles() {
		List<Article> articles = blogService.findAll();
		if(articles.isEmpty()) {
			//return new ApiUtil<>(new Exception400("게시글이 존재하지 않습니다."));
			throw new Exception400("게시글이 존재하지 않습니다.");
		}
		return new ApiUtil<>(articles);
	}
	
}