-
Notifications
You must be signed in to change notification settings - Fork 1
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
문화축제 API 호출입니다. #5
Open
dev-khg
wants to merge
3
commits into
main
Choose a base branch
from
feat-hg-json-parsing
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
3 commits
Select commit
Hold shift + click to select a range
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 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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/catcher/batch/openapi/jsonparser/CatcherJsonParser.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,41 @@ | ||
package com.catcher.batch.openapi.jsonparser; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.json.JSONObject; | ||
|
||
public abstract class CatcherJsonParser { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private String headerPath; | ||
private String bodyPath; | ||
|
||
public CatcherJsonParser(String headerPath, String bodyPath) { | ||
this.headerPath = headerPath; | ||
this.bodyPath = bodyPath; | ||
} | ||
|
||
public <H extends JsonHeader> H parseHeader(String json ,Class<H> clazz) throws JsonProcessingException { | ||
JSONObject headerJson = getJsonObject(json, headerPath); | ||
return objectMapper.readValue(headerJson.toString(), clazz); | ||
} | ||
|
||
public <B extends JsonBody> B parseBody(String json ,Class<B> clazz) throws JsonProcessingException { | ||
JSONObject bodyJson = getJsonObject(json, bodyPath); | ||
return objectMapper.readValue(bodyJson.toString(), clazz); | ||
} | ||
|
||
private JSONObject getJsonObject(String json, String path) { | ||
JSONObject jsonObject = new JSONObject(json); | ||
if (path != null) { | ||
String[] split = path.split("\\."); | ||
|
||
for (String s : split) { | ||
jsonObject = jsonObject.getJSONObject(s); | ||
} | ||
} | ||
return jsonObject; | ||
} | ||
|
||
public abstract String getUri(int page, int count); | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/catcher/batch/openapi/jsonparser/JsonBody.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,6 @@ | ||
package com.catcher.batch.openapi.jsonparser; | ||
|
||
|
||
public interface JsonBody { | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/catcher/batch/openapi/jsonparser/JsonHeader.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,6 @@ | ||
package com.catcher.batch.openapi.jsonparser; | ||
|
||
|
||
public interface JsonHeader { | ||
boolean validate(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/catcher/batch/openapi/jsonparser/festival/FestivalBody.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.catcher.batch.openapi.jsonparser.festival; | ||
|
||
import com.catcher.batch.openapi.jsonparser.JsonBody; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class FestivalBody implements JsonBody { | ||
@JsonProperty("items") | ||
private List<FestivalItem> items; | ||
@JsonProperty("totalCount") | ||
private Integer totalCount; | ||
@JsonProperty("numOfRows") | ||
private Integer numOfRows; | ||
@JsonProperty("pageNo") | ||
private Integer pageNo; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/catcher/batch/openapi/jsonparser/festival/FestivalHeader.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,21 @@ | ||
package com.catcher.batch.openapi.jsonparser.festival; | ||
|
||
import com.catcher.batch.openapi.jsonparser.JsonHeader; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
|
||
|
||
@Getter | ||
public class FestivalHeader implements JsonHeader { | ||
@JsonProperty("resultCode") | ||
private String resultCode; | ||
@JsonProperty("resultMsg") | ||
private String resultMsg; | ||
@JsonProperty("type") | ||
private String type; | ||
|
||
@Override | ||
public boolean validate() { | ||
return resultCode.equals("00"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/catcher/batch/openapi/jsonparser/festival/FestivalItem.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,23 @@ | ||
package com.catcher.batch.openapi.jsonparser.festival; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
|
||
import java.util.Date; | ||
|
||
@Getter | ||
@JsonIgnoreProperties(ignoreUnknown=true) | ||
public class FestivalItem { | ||
@JsonProperty("fstvlNm") | ||
private String name; | ||
|
||
@JsonProperty("rdnmadr") | ||
private String address; | ||
|
||
@JsonProperty("fstvlStartDate") | ||
private Date startDate; | ||
|
||
@JsonProperty("fstvlEndDate") | ||
private Date endDate; | ||
} |
52 changes: 52 additions & 0 deletions
52
src/test/java/com/catcher/batch/openapi/jsonparser/festival/FestivalJsonParserTest.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,52 @@ | ||
package com.catcher.batch.openapi.jsonparser.festival; | ||
|
||
import com.catcher.batch.openapi.jsonparser.CatcherJsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.springframework.http.HttpMethod.GET; | ||
|
||
class FestivalJsonParserTest { | ||
|
||
|
||
@Test | ||
void 축제_데이터_파싱() throws JsonProcessingException { | ||
String headerPath = "response.header"; | ||
String bodyPath = "response.body"; | ||
String endPoint = "http://api.data.go.kr/openapi/tn_pubr_public_cltur_fstvl_api"; | ||
String serviceKey = ""; | ||
|
||
CatcherJsonParser festivalParser = new CatcherJsonParser(headerPath, bodyPath) { | ||
@Override | ||
public String getUri(int page, int count) { | ||
return new StringBuilder() | ||
.append(endPoint).append("?") | ||
.append("serviceKey=").append(serviceKey) | ||
.append("&").append("pageNo=").append(page) | ||
.append("&").append("numOfRows=").append(count) | ||
.append("&").append("type=").append("json").toString(); | ||
} | ||
}; | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpHeaders header = new HttpHeaders(); | ||
HttpEntity<?> entity = new HttpEntity<>(header); | ||
|
||
//when | ||
ResponseEntity<String> exchange = restTemplate.exchange(festivalParser.getUri(1, 5), GET, entity, String.class); | ||
String body = exchange.getBody(); | ||
|
||
//then | ||
FestivalHeader festivalHeader = festivalParser.parseHeader(body, FestivalHeader.class); | ||
assertThat(festivalHeader.validate()).isTrue(); | ||
FestivalBody festivalBody = festivalParser.parseBody(body, FestivalBody.class); | ||
assertThat(festivalBody.getItems().size()).isEqualTo(5); | ||
} | ||
} |
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.
요부분은 실제로 사용하실때는 UriBuilder를 이용하여 만들면 더 좋을거같습니다~