diff --git a/src/main/java/com/example/globalTimes_be/domain/news/controller/AiController.java b/src/main/java/com/example/globalTimes_be/domain/news/controller/AiController.java index 29a1475..d8bac69 100644 --- a/src/main/java/com/example/globalTimes_be/domain/news/controller/AiController.java +++ b/src/main/java/com/example/globalTimes_be/domain/news/controller/AiController.java @@ -1,10 +1,12 @@ package com.example.globalTimes_be.domain.news.controller; +import com.example.globalTimes_be.domain.news.exception.NewsErrorStatus; import com.example.globalTimes_be.domain.news.service.AiService; import com.example.globalTimes_be.domain.news.service.AiSseService; import com.example.globalTimes_be.domain.news.service.NewsService; import com.example.globalTimes_be.global.apiPayload.code.ApiResponse; import com.example.globalTimes_be.global.apiPayload.code.status.GlobalSuccessStatus; +import com.example.globalTimes_be.global.exception.BaseException; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -28,6 +30,14 @@ public ResponseEntity summarizeArticle(@PathVariable Long id, //해당 기사 내용을 가져옴 String crawledContent = newsService.getArticleCrawledContent(id); + //크롤링한 기사 내용이 없으면 본문 서두 반환 + if (crawledContent == null) { + //기사 id에 맞는 content 가져옴 + String content = newsService.getArticleContent(id); + + return ApiResponse.fail(NewsErrorStatus._CRAWLER_ERROR.getResponse(), content); + } + //해당 기사를 요약함 String summary = aiService.summarizeArticle(crawledContent, language); @@ -42,6 +52,11 @@ public SseEmitter summarizeArticleSse(@PathVariable Long id, //해당 기사 내용을 가져옴 String crawledContent = newsService.getArticleCrawledContent(id); + //크롤링한 기사 내용이 없으면 에러 처리 + if (crawledContent == null) { + throw new BaseException(NewsErrorStatus._CRAWLER_ERROR.getResponse()); + } + //GPT 요약 요청 후 SSE 스트리밍 전송 return aiSseService.summarizeContent(crawledContent, language); } @@ -53,7 +68,11 @@ public SseEmitter askArticle(@PathVariable Long id, @RequestParam String question){ //해당 기사 내용을 가져옴 String crawledContent = newsService.getArticleCrawledContent(id); - + + //크롤링한 기사 내용이 없으면 에러 처리 + if (crawledContent == null) { + throw new BaseException(NewsErrorStatus._CRAWLER_ERROR.getResponse()); + } //GPT 답변 요청 후 SSE 스트리밍 전송 return aiSseService.askGPT(crawledContent, question); } diff --git a/src/main/java/com/example/globalTimes_be/domain/news/controller/AiControllerDocs.java b/src/main/java/com/example/globalTimes_be/domain/news/controller/AiControllerDocs.java index c4dc930..b58e46c 100644 --- a/src/main/java/com/example/globalTimes_be/domain/news/controller/AiControllerDocs.java +++ b/src/main/java/com/example/globalTimes_be/domain/news/controller/AiControllerDocs.java @@ -39,11 +39,11 @@ public interface AiControllerDocs { content = @Content(mediaType = "application/json", examples = @ExampleObject(value = """ { - "timestamp": "2024-10-30T15:38:12.43483271", - "isSuccess": false, - "message": "해당 언론사는 요약 정보 제공이 불가능합니다.", - "data": null - } + "timestamp": "2025-04-01T13:37:56.6982049", + "isSuccess": false, + "message": "해당 언론사는 요약 정보 제공이 불가능합니다.", + "data": "NEWARK With a mastery of collaborative, often pretty basketball that belied both its youth and the volatile state of the college sport, Duke soared to the programs 18th Final Four on Saturday night, … [+5884 chars]" + } """) ) ), diff --git a/src/main/java/com/example/globalTimes_be/domain/news/controller/NewsControllerDocs.java b/src/main/java/com/example/globalTimes_be/domain/news/controller/NewsControllerDocs.java index 097fa33..bef57b1 100644 --- a/src/main/java/com/example/globalTimes_be/domain/news/controller/NewsControllerDocs.java +++ b/src/main/java/com/example/globalTimes_be/domain/news/controller/NewsControllerDocs.java @@ -30,6 +30,7 @@ public interface NewsControllerDocs { "sourceName": "Politico", "author": "Seb Starcevic", "title": "Trump floats possibility of compensation for Jan. 6 rioters - POLITICO", + "viewCount": 1, "url": "https://www.politico.com/news/2025/03/25/trump-floats-possibility-of-compensation-for-jan-6-rioters-00250063", "urlToImage": "https://static.politico.com/7a/af/06aa55284352997b83a000983381/trump-63887.jpg", "publishedAt": "2025-03-26T03:00:31Z" diff --git a/src/main/java/com/example/globalTimes_be/domain/news/service/NewsService.java b/src/main/java/com/example/globalTimes_be/domain/news/service/NewsService.java index df9ebc9..b19708c 100644 --- a/src/main/java/com/example/globalTimes_be/domain/news/service/NewsService.java +++ b/src/main/java/com/example/globalTimes_be/domain/news/service/NewsService.java @@ -74,6 +74,15 @@ public NewsResDTO getNewsDetail(Long id) { .build(); } + //크롤링 실패 시 본문 서두 반환 + public String getArticleContent(Long id) { + //뉴스 정보 가져옴 + Article article = articleRepository.findById(id) + .orElseThrow(() -> new BaseException(NewsErrorStatus._EMPTY_NEWS_DATA.getResponse())); + + return article.getContent(); + } + //DB에 뉴스 기사가 없으면 원본 뉴스 사이트에서 크롤링해옴 @Transactional public String getArticleCrawledContent(Long id) { @@ -91,9 +100,9 @@ public String getArticleCrawledContent(Long id) { // 크롤러로 기사 원문 가져옴 crawledContent = getCrawlerUrl(article.getUrl()); - // 크롤링 실패시 에러처리 + // 크롤링 실패시 return; if (crawledContent == null) { - throw new BaseException(NewsErrorStatus._CRAWLER_ERROR.getResponse()); + return null; } //DB에 저장