-
Notifications
You must be signed in to change notification settings - Fork 1
Feign client 이용하여 tour API 호출 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pingu9
wants to merge
6
commits into
main
Choose a base branch
from
feat-dj-tour-API-job
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e9ac7e
chore: Spring cloud, batch, feign 세팅
227b245
chore: datasource 설정 추가
7844ec4
feat: feign client 이용하여 tour API 호출 추가(임시)
0b50aab
Merge branch 'main' into feat-dj-tour-API-job
60c07e8
feat: feign client에 Response converter 적용
ec37ae9
feat: feign call에 config 적용
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/catcher/batch/annotation/CatcherJson.java
This file contains hidden or 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,13 @@ | ||
package com.catcher.batch.annotation; | ||
|
||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface CatcherJson { | ||
String path(); | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/catcher/batch/core/converter/CatcherConverter.java
This file contains hidden or 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,46 @@ | ||
package com.catcher.batch.core.converter; | ||
|
||
import com.catcher.batch.annotation.CatcherJson; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.json.JSONObject; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CatcherConverter { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
public <T> T parse(String jsonMessage, Class<T> responseType) { | ||
String path = getPath(responseType); | ||
JSONObject jsonObject = getJsonObject(jsonMessage, path); | ||
|
||
try { | ||
return objectMapper.readValue(jsonObject.toString(), responseType); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private <T> String getPath(Class<T> responseType) { | ||
|
||
CatcherJson annotation = responseType.getAnnotation(CatcherJson.class); | ||
if(annotation == null) { | ||
throw new IllegalStateException(); | ||
} | ||
return annotation.path(); | ||
} | ||
|
||
private JSONObject getJsonObject(String json, String jsonPath) { | ||
if(jsonPath == null) { | ||
throw new IllegalStateException(); | ||
} | ||
|
||
JSONObject jsonObject = new JSONObject(json); | ||
String[] subPath = jsonPath.split("\\."); | ||
|
||
for (String path : subPath) { | ||
jsonObject = jsonObject.getJSONObject(path); | ||
} | ||
return jsonObject; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/catcher/batch/external/TourApiServiceExternalCallService.java
This file contains hidden or 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,15 @@ | ||
package com.catcher.batch.external; | ||
|
||
import com.catcher.batch.external.config.CatcherFeignClientCommonConfig; | ||
import com.catcher.batch.external.vo.request.TourApiRequest; | ||
import com.catcher.batch.external.vo.response.TourApiResponse; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.cloud.openfeign.SpringQueryMap; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@FeignClient(name = "TOUR-API-SERVICE", url = "http://apis.data.go.kr/B551011/KorService1", configuration = CatcherFeignClientCommonConfig.class) | ||
public interface TourApiServiceExternalCallService { | ||
|
||
@GetMapping("/searchFestival1") | ||
TourApiResponse callTourList(@SpringQueryMap TourApiRequest request); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/catcher/batch/external/config/CatcherFeignClientCommonConfig.java
This file contains hidden or 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,31 @@ | ||
package com.catcher.batch.external.config; | ||
|
||
import com.catcher.batch.core.converter.CatcherConverter; | ||
import feign.RequestInterceptor; | ||
import feign.ResponseInterceptor; | ||
import feign.Util; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
|
||
public class CatcherFeignClientCommonConfig { | ||
|
||
public static final String APPLICATION_FORM_URLENCODED_UTF8_VALUE = | ||
MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=utf-8"; | ||
|
||
@Bean | ||
public RequestInterceptor requestInterceptor() { | ||
|
||
return requestTemplate -> requestTemplate.header(HttpHeaders.CONTENT_TYPE, APPLICATION_FORM_URLENCODED_UTF8_VALUE); | ||
} | ||
|
||
@Bean | ||
public ResponseInterceptor responseInterceptor(CatcherConverter catcherConverter) { | ||
|
||
return invocationContext -> { | ||
Class<?> responseType = (Class<?>) invocationContext.returnType(); | ||
String responseBody = Util.toString(invocationContext.response().body().asReader()); | ||
return catcherConverter.parse(responseBody, responseType); | ||
}; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/catcher/batch/external/vo/request/TourApiRequest.java
This file contains hidden or 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,27 @@ | ||
package com.catcher.batch.external.vo.request; | ||
|
||
import lombok.Getter; | ||
|
||
/* TODO: 하드코딩된 값 바꾸기 */ | ||
|
||
@Getter | ||
public class TourApiRequest { | ||
|
||
private Integer numOfRows = 100; | ||
|
||
private Integer pageNo = 1; | ||
|
||
private String MobileOS = "ETC"; | ||
|
||
private String MobileApp = "AppTest"; | ||
|
||
private String _type = "json"; | ||
|
||
private String listYN = "Y"; | ||
|
||
private String arrange = "A"; | ||
|
||
private String eventStartDate = "20230901"; | ||
|
||
private String serviceKey = "your_service_key"; | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/com/catcher/batch/external/vo/response/TourApiResponse.java
This file contains hidden or 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,70 @@ | ||
package com.catcher.batch.external.vo.response; | ||
|
||
import com.catcher.batch.annotation.CatcherJson; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@CatcherJson(path = "response.body.items") | ||
public class TourApiResponse { | ||
|
||
private List<TourApiItem> item; | ||
|
||
// private InnerTourAPiResponse response; | ||
// | ||
// @Getter | ||
// public static class InnerTourAPiResponse { | ||
// | ||
// private TourApiHeader header; | ||
// | ||
// private TourApiBody body; | ||
// } | ||
// | ||
// @Getter | ||
// public static class TourApiHeader { | ||
// private String resultCode; | ||
// private String resultMsg; | ||
// } | ||
// | ||
// @Getter | ||
// public static class TourApiBody { | ||
// | ||
// private TourApiItems items; | ||
// private Integer numOfRows; | ||
// private Integer pageNo; | ||
// private Integer totalCount; | ||
// } | ||
// | ||
// @Getter | ||
// public static class TourApiItems { | ||
// private List<TourApiItem> item; | ||
// } | ||
|
||
/* TODO: JsonProperty 어노테이션 이용하여 적당한 변수명으로 바꾸기 */ | ||
@Getter | ||
public static class TourApiItem { | ||
private String addr1; | ||
private String addr2; | ||
private String booktour; | ||
private String cat1; | ||
private String cat2; | ||
private String cat3; | ||
private String contentid; | ||
private String contenttypeid; | ||
private String createdtime; | ||
private String eventstartdate; | ||
private String eventenddate; | ||
private String firstimage; | ||
private String firstimage2; | ||
private String cpyrhtDivCd; | ||
private String mapx; | ||
private String mapy; | ||
private String mlevel; | ||
private String modifiedtime; | ||
private String areacode; | ||
private String sigungucode; | ||
private String tel; | ||
private String title; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/catcher/batch/job/config/CommonJobConfig.java
This file contains hidden or 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,10 @@ | ||
package com.catcher.batch.job.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
|
||
@RequiredArgsConstructor | ||
public class CommonJobConfig { | ||
|
||
protected final JobLauncher jobLauncher; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/catcher/batch/job/config/TourApiJob.java
This file contains hidden or 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,22 @@ | ||
package com.catcher.batch.job.config; | ||
|
||
import com.catcher.batch.external.TourApiServiceExternalCallService; | ||
import com.catcher.batch.external.vo.request.TourApiRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class TourApiJob { | ||
|
||
private final TourApiServiceExternalCallService tourApiServiceExternalCallService; | ||
|
||
/* TODO: Spring batch 이용하도록 변경? */ | ||
@Scheduled(fixedDelay = 1000000) //TODO: 시간대 정해서 하루에 1번으로 변경 | ||
public void tourApiJob() { | ||
|
||
final var response = tourApiServiceExternalCallService.callTourList(new TourApiRequest()); | ||
cheolwon1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} |
This file contains hidden or 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,27 @@ | ||
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver | ||
|
||
## spring datasource | ||
spring.datasource.url=${DATASOURCE_URL} | ||
spring.datasource.username=${DATASOURCE_USERNAME} | ||
spring.datasource.password=${DATASOURCE_PASSWORD} | ||
|
||
|
||
## ssh | ||
ssh.host=${SSH_HOST} | ||
ssh.port=${SSH_PORT} | ||
ssh.username=${SSH_USERNAME} | ||
ssh.password=${SSH_PASSWORD} | ||
ssh.local-port=${SSH_LOCAL_PORT} | ||
ssh.datasource.origin=${SSH_DATASOURCE_ORIGIN} | ||
|
||
#update the schema with the given values. | ||
spring.jpa.hibernate.ddl-auto=update | ||
#To beautify or pretty print the SQL | ||
spring.jpa.properties.hibernate.format_sql=true | ||
#show sql | ||
spring.jpa.properties.hibernate.show-sql=true | ||
#show parameter binding | ||
logging.level.org.hibernate.type.descriptor.sql=DEBUG | ||
|
||
logging.level.org.hibernate.SQL=DEBUG | ||
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MariaDBDialect |
This file contains hidden or 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,27 @@ | ||
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver | ||
|
||
## spring datasource | ||
spring.datasource.url=${DATASOURCE_URL} | ||
spring.datasource.username=${DATASOURCE_USERNAME} | ||
spring.datasource.password=${DATASOURCE_PASSWORD} | ||
|
||
|
||
## ssh | ||
ssh.host=${SSH_HOST} | ||
ssh.port=${SSH_PORT} | ||
ssh.username=${SSH_USERNAME} | ||
ssh.password=${SSH_PASSWORD} | ||
ssh.local-port=${SSH_LOCAL_PORT} | ||
ssh.datasource.origin=${SSH_DATASOURCE_ORIGIN} | ||
|
||
#update the schema with the given values. | ||
spring.jpa.hibernate.ddl-auto=update | ||
#To beautify or pretty print the SQL | ||
spring.jpa.properties.hibernate.format_sql=true | ||
#show sql | ||
spring.jpa.properties.hibernate.show-sql=true | ||
#show parameter binding | ||
logging.level.org.hibernate.type.descriptor.sql=DEBUG | ||
|
||
logging.level.org.hibernate.SQL=DEBUG | ||
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MariaDBDialect |
This file contains hidden or 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,27 @@ | ||
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver | ||
|
||
## spring datasource | ||
spring.datasource.url=${DATASOURCE_URL} | ||
spring.datasource.username=${DATASOURCE_USERNAME} | ||
spring.datasource.password=${DATASOURCE_PASSWORD} | ||
|
||
|
||
## ssh | ||
ssh.host=${SSH_HOST} | ||
ssh.port=${SSH_PORT} | ||
ssh.username=${SSH_USERNAME} | ||
ssh.password=${SSH_PASSWORD} | ||
ssh.local-port=${SSH_LOCAL_PORT} | ||
ssh.datasource.origin=${SSH_DATASOURCE_ORIGIN} | ||
|
||
#update the schema with the given values. | ||
spring.jpa.hibernate.ddl-auto=update | ||
#To beautify or pretty print the SQL | ||
spring.jpa.properties.hibernate.format_sql=true | ||
#show sql | ||
spring.jpa.properties.hibernate.show-sql=true | ||
#show parameter binding | ||
logging.level.org.hibernate.type.descriptor.sql=DEBUG | ||
|
||
logging.level.org.hibernate.SQL=DEBUG | ||
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MariaDBDialect |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
홍근님께서 올려주신 PR 참고하여 response converter를 적용해보았습니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feign Inteceptor를 통한 공통처리 좋네요!!