-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨Feat: 스크랩 데이터 제공 API
- Loading branch information
Showing
9 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/com/korad1004/back_end/config/RestClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.korad1004.back_end.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Configuration | ||
public class RestClientConfig { | ||
|
||
@Bean | ||
public RestClient restClient(RestClient.Builder builder) { | ||
return builder.build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/korad1004/back_end/global/handler/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.korad1004.back_end.global.handler; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||
public ResponseEntity<String> handleTypeMismatchException(MethodArgumentTypeMismatchException exception) { | ||
String errorMessage = "잘못된 요청: '" + exception.getValue() + "'의 타입이 잘못되었습니다."; | ||
return ResponseEntity.badRequest().body(errorMessage); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/korad1004/back_end/scrap/controller/KoradNaverBlogScrapController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.korad1004.back_end.scrap.controller; | ||
|
||
import com.korad1004.back_end.scrap.dto.KoradNaverBlogRecentArticleDto; | ||
import com.korad1004.back_end.scrap.service.ScrapService; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@Tag(name = "scrap-controller") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/scrap/korad-naver-blog") | ||
@Slf4j | ||
public class KoradNaverBlogScrapController { | ||
|
||
private final ScrapService scrapService; | ||
|
||
@Value("${lambda.api.korad-naver-blog.article.recent}") | ||
private String apiUrl; | ||
|
||
@GetMapping("/article/recent/{cnt}") | ||
public ResponseEntity<?> getKoradNaverBlogRecentArticle(@PathVariable Integer cnt) { | ||
if (cnt <= 0) { | ||
return ResponseEntity.badRequest().body("cnt는 0 이하의 숫자값을 가질 수 없습니다."); | ||
} | ||
|
||
String url = apiUrl + "?cnt=" + cnt; | ||
|
||
return scrapService.getScrapResponse(url, KoradNaverBlogRecentArticleDto.class); | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/korad1004/back_end/scrap/controller/KoradNoticeScrapController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.korad1004.back_end.scrap.controller; | ||
|
||
import com.korad1004.back_end.scrap.dto.KoradNoticeRecentArticleDto; | ||
import com.korad1004.back_end.scrap.service.ScrapService; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@Tag(name = "scrap-controller") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/scrap/korad-official-website") | ||
public class KoradNoticeScrapController { | ||
|
||
private final ScrapService scrapService; | ||
|
||
@Value("${lambda.api.korad-official-website.notice.recent}") | ||
private String apiUrl; | ||
|
||
@GetMapping("/notice/recent/{cnt}") | ||
public ResponseEntity<?> getKoradOfficialWebsiteNoticeRecent(@PathVariable Integer cnt) { | ||
if (cnt <= 0) { | ||
return ResponseEntity.badRequest().body("cnt는 0 이하의 숫자값을 가질 수 없습니다."); | ||
} | ||
|
||
String url = apiUrl + "?cnt=" + cnt; | ||
|
||
return scrapService.getScrapResponse(url, KoradNoticeRecentArticleDto.class); | ||
|
||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/korad1004/back_end/scrap/controller/RadiationScrapController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.korad1004.back_end.scrap.controller; | ||
|
||
import com.korad1004.back_end.scrap.dto.NationWideRadiationAverageDto; | ||
import com.korad1004.back_end.scrap.service.ScrapService; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@Tag(name = "scrap-controller") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/scrap/radiation") | ||
public class RadiationScrapController { | ||
|
||
private final ScrapService scrapService; | ||
|
||
@Value("${lambda.api.radiation.average}") | ||
private String apiUrl; | ||
|
||
@GetMapping("/average") | ||
public ResponseEntity<?> getRadiationAverage() { | ||
|
||
String url = apiUrl; | ||
|
||
return scrapService.getScrapResponse(url, NationWideRadiationAverageDto.class); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/korad1004/back_end/scrap/dto/KoradNaverBlogRecentArticleDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.korad1004.back_end.scrap.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class KoradNaverBlogRecentArticleDto { | ||
|
||
private int idx; | ||
private Content content; | ||
|
||
@Data | ||
public static class Content { | ||
private String notice_num; | ||
private String title; | ||
private String content; | ||
private String write_date; | ||
private String url; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/korad1004/back_end/scrap/dto/KoradNoticeRecentArticleDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.korad1004.back_end.scrap.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class KoradNoticeRecentArticleDto { | ||
private int idx; | ||
private KoradNaverBlogRecentArticleDto.Content content; | ||
|
||
@Data | ||
public static class Content { | ||
private String notice_num; | ||
private String title; | ||
private String content; | ||
private String write_date; | ||
private String url; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/korad1004/back_end/scrap/dto/NationWideRadiationAverageDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.korad1004.back_end.scrap.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class NationWideRadiationAverageDto { | ||
@JsonProperty(value = "지역") | ||
private String region; | ||
private Double avgRad; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/korad1004/back_end/scrap/service/ScrapService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.korad1004.back_end.scrap.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ScrapService { | ||
private final RestClient restClient; | ||
|
||
public <T> ResponseEntity<?> getScrapResponse(String url, Class<T> dto) { | ||
try { | ||
ResponseEntity<List<T>> responseEntity = restClient.get() | ||
.uri(url) | ||
.retrieve() | ||
.toEntity(new ParameterizedTypeReference<>() {}); | ||
|
||
|
||
if (!responseEntity.getStatusCode().is2xxSuccessful()) { | ||
return ResponseEntity.status(responseEntity.getStatusCode()).body(responseEntity.getBody()); | ||
} | ||
|
||
List<?> data = responseEntity.getBody(); | ||
if (data == null || data.isEmpty()) { | ||
log.error("Response 에러: {}", data); | ||
return ResponseEntity.internalServerError().body("스크랩 API 서버의 요청에 문제가 발생하였습니다."); | ||
} | ||
|
||
return ResponseEntity.ok(data); | ||
} catch (Exception e) { | ||
log.error("getScrapResponse 에러 : {}", e.getMessage()); | ||
return ResponseEntity.status(500).body("스크랩 API 서버와의 통신에 실패하였습니다."); | ||
} | ||
} | ||
} |