๐ก
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);
}
}