์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[JPA] ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ API ๋งŒ๋“ค์–ด ๋ณด๊ธฐ - 5

๋ฏธ๋กœ910 2024. 10. 2. 12:43
๐Ÿ’ก
1. ๊ธ€ ์ „์ฒด ์กฐํšŒ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
2. ์‘๋‹ต ์ฒ˜๋ฆฌ์‹œ ๊ณตํ†ต DTO๋ฅผ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.
3. ExceptionHandler ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

BlogService ํŒŒ์ผ์— ๊ฒŒ์‹œ๊ธ€ ์ „์ฒด ์กฐํšŒ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ํ•˜๊ธฐ - 1
@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;
	}
    }

 

๊ณตํ†ต ์‘๋‹ต DTO ๋งŒ๋“ค๊ธฐ - 2
@Data
public class ApiUtil<T> {

	private Integer status; // ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ ์ €์žฅ(200, 400, 500)
	private String msg; // ์‘๋‹ต ๋ฉ”์‹œ์ง€ ์ €์žฅ(์„ฑ๊ณต, ์‹คํŒจ ๋ฌธ์ž์—ด)
	private T body; // ์‘๋‹ต์˜ ์‹ค์ œ ๋ฐ์ดํ„ฐ ์ €์žฅ (์ œ๋„ค๋ฆญ ํ™œ์šฉ)
	
	// ์„ฑ๊ณต์ ์ธ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์„ฑ์ž
	public ApiUtil(T body) {
		this.status = 200; // ํ†ต์‹  ์„ฑ๊ณต
		this.msg = "์„ฑ๊ณต";
		this.body = body;
	}
	
	// ์ปค์Šคํ…€ ์ƒํƒœ ์ฝ”๋“œ์™€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ˜ํ™˜ ์‹œ๊ธธ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์„ฑ์ž (์˜ˆ : ์—๋Ÿฌ ์‘๋‹ต)
	public ApiUtil(Integer status, String msg) {
		this.status = status;
		this.msg = msg;
		this.body = null;
	}
	
}

 

BlogController ์ฝ”๋“œ ์ถ”๊ฐ€ - 3

@RequiredArgsConstructor
@RestController // @controller + @responsebody
public class BlogApiController {
	
	private final BlogService blogService;
		
	// URL , ์ฆ‰, ์ฃผ์†Œ ์„ค๊ณ„ - http://localhost:8080/api/article
	@PostMapping("/api/articles")
	public ResponseEntity<Article> addArticle(@RequestBody ArticleDTO dto) {
		// 1. ์ธ์ฆ ๊ฒ€์‚ฌ
		// 2. ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ 
		Article savedArtilce = blogService.save(dto);
		return ResponseEntity.status(HttpStatus.CREATED).body(savedArtilce);
	}
	
	
	// URL , ์ฆ‰, ์ฃผ์†Œ ์„ค๊ณ„ - http://localhost:8080/api/articles
	@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);
	}
	
}

 

ExceptionHandler ๋งŒ๋“ค๊ธฐ - 4

์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ
public class Exception400 extends RuntimeException {

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

 

ExceptionHandler ๋งŒ๋“ค๊ธฐ
// 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);
	}

}