Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.json:json:20190722'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.12.5'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.5'

annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Expand Down
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);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.catcher.batch.openapi.jsonparser;


public interface JsonBody {

}
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();
}
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;
}
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");
}
}
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;
}
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요부분은 실제로 사용하실때는 UriBuilder를 이용하여 만들면 더 좋을거같습니다~

.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);
}
}