▶ XML to JSON을 하던 도중 'ProtoApplication' 에서 Object.class를 ResponseDTO로 수정하니까 오류가 떴다
▶ API REST 출력결과 예제를 보고 DTO 설계를 했는데 알고보니 아예 다른 형식으로 날아왔다
ProtoApplication
try{
String xml = str;
JSONObject jObject = XML.toJSONObject(xml);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Object json = mapper.readValue(jObject.toString(), ResponseDTO.class);
String output = mapper.writeValueAsString(json);
System.out.println(output);
}catch (Exception e) {
e.printStackTrace();
}
실제 날아오는 JSON 형식
{
"response" : {
"header" : {
"resultCode" : "00",
"resultMsg" : "NORMAL_CODE"
},
"body" : {
"items" : {
"item" : [ {
"acc_year" : 2023,
"occrrnc_dt" : 2.023011201E9,
"dght_cd" : 2,
"occrrnc_day_cd" : 5,
"dth_dnv_cnt" : 1,
"injpsn_cnt" : 1,
"se_dnv_cnt" : 0,
"sl_dnv_cnt" : 0,
"wnd_dnv_cnt" : 0,
"occrrnc_lc_sido_cd" : 1100,
"occrrnc_lc_sgg_cd" : 1117,
"acc_ty_lclas_cd" : "03",
"acc_ty_mlsfc_cd" : 31,
"acc_ty_cd" : 32,
"aslt_vtr_cd" : "05",
"road_frm_lclas_cd" : "01",
"road_frm_cd" : "05",
"wrngdo_isrty_vhcty_lclas_cd" : "01",
"dmge_isrty_vhcty_lclas_cd" : "##",
"lo_crd" : 127.17917269,
"la_crd" : 37.55018177,
"occrrnc_lc_x_crd" : 971661,
"occrrnc_lc_y_crd" : 1950142
}, {
"acc_year" : 2023,
"occrrnc_dt" : 2.023031011E9,
"dght_cd" : 1,
"occrrnc_day_cd" : 6,
"dth_dnv_cnt" : 1,
"injpsn_cnt" : 1,
"se_dnv_cnt" : 0,
"sl_dnv_cnt" : 0,
"wnd_dnv_cnt" : 0,
"occrrnc_lc_sido_cd" : 1100,
"occrrnc_lc_sgg_cd" : 1117,
"acc_ty_lclas_cd" : "02",
"acc_ty_mlsfc_cd" : 22,
"acc_ty_cd" : 22,
"aslt_vtr_cd" : "03",
"road_frm_lclas_cd" : "01",
"road_frm_cd" : "05",
"wrngdo_isrty_vhcty_lclas_cd" : "03",
"dmge_isrty_vhcty_lclas_cd" : "05",
"lo_crd" : 127.14164953,
"la_crd" : 37.51817484,
"occrrnc_lc_x_crd" : 968333,
"occrrnc_lc_y_crd" : 1946603
}, {
"acc_year" : 2023,
"occrrnc_dt" : 2.023070901E9,
"dght_cd" : 2,
"occrrnc_day_cd" : 1,
"dth_dnv_cnt" : 1,
"injpsn_cnt" : 2,
"se_dnv_cnt" : 0,
"sl_dnv_cnt" : 1,
"wnd_dnv_cnt" : 0,
"occrrnc_lc_sido_cd" : 1100,
"occrrnc_lc_sgg_cd" : 1117,
"acc_ty_lclas_cd" : "02",
"acc_ty_mlsfc_cd" : 21,
"acc_ty_cd" : 21,
"aslt_vtr_cd" : "03",
"road_frm_lclas_cd" : "02",
"road_frm_cd" : "06",
"wrngdo_isrty_vhcty_lclas_cd" : "02",
"dmge_isrty_vhcty_lclas_cd" : "05",
"lo_crd" : 127.14450893,
"la_crd" : 37.55469743,
"occrrnc_lc_x_crd" : 968654,
"occrrnc_lc_y_crd" : 1950647
}, {
"acc_year" : 2023,
"occrrnc_dt" : 2.023081502E9,
"dght_cd" : 2,
"occrrnc_day_cd" : 3,
"dth_dnv_cnt" : 1,
"injpsn_cnt" : 1,
"se_dnv_cnt" : 0,
"sl_dnv_cnt" : 0,
"wnd_dnv_cnt" : 0,
"occrrnc_lc_sido_cd" : 1100,
"occrrnc_lc_sgg_cd" : 1117,
"acc_ty_lclas_cd" : "02",
"acc_ty_mlsfc_cd" : 23,
"acc_ty_cd" : 23,
"aslt_vtr_cd" : "04",
"road_frm_lclas_cd" : "01",
"road_frm_cd" : "05",
"wrngdo_isrty_vhcty_lclas_cd" : "03",
"dmge_isrty_vhcty_lclas_cd" : "03",
"lo_crd" : 127.17435537,
"la_crd" : 37.56194027,
"occrrnc_lc_x_crd" : 970955,
"occrrnc_lc_y_crd" : 1951953
} ]
},
"pageNo" : 1,
"totalCount" : 4,
"numOfRows" : 4
}
}
}
Response
package com.carblre.dto;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;
/**
* 법규위반별 사고다발지역 API
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class Response {
private HeaderDTO header;
private BodyDTO body;
}
HeaderDTO
package com.carblre.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;
/**
* 법규위반별 사고다발지역 API
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class HeaderDTO {
@JsonProperty("resultCode")
private String resultCode;
@JsonProperty("resultMsg")
private String resultMsg;
}
BodyDTO
package com.carblre.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;
/**
* 법규위반별 사고다발지역 API
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class BodyDTO {
@JsonProperty("pageNo")
private int pageNo;
@JsonProperty("totalCount")
private int totalCount;
private ItemsDTO items;
@JsonProperty("numOfRows")
private int numOfRows;
}
ItemsDTO
package com.carblre.dto;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;
import java.util.List;
/**
* 법규위반별 사고다발지역 API
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ItemsDTO {
private List<ItemDTO> item;
}
ItemDTO
package com.carblre.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;
/**
* 법규위반별 사고다발지역 API
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ItemDTO {
private int accYear; // 사고년도
private Double occrrncDt; // 월일시
private int dghtCd; // 주야구분코드
private int occrrncDayCd; // 요일코드
private int dthDnvCnt; // 사망자수
private int injpsnCnt; // 부상자수
private int seDnvCnt; // 중상자수
private int slDnvCnt; // 경상자수
private int wndDnvCnt; // 부상신고자수
private int occrrncLcSidoCd; // 위치 시도 코드
private int occrrncLcSggCd; // 위치 시군구 코드
private String accTyLclasCd; // 사고유형 대분류 코드
private int accTyMlsfcCd; // 사고유형 중분류 코드
private int accTyCd; // 사고유형 코드
private String asltVtrCd; // 가해자 법규위반 코드
private String roadFrmLclasCd; // 도로형태 대분류 코드
private String roadFrmCd; // 도로형태 코드
private String wrngdoIsrtyVhctyLclasCd;
private String dmgeIsrtyVhctyLclasCd; // 피해당사자 차종별 대분류 코드
@JsonProperty("occrrnc_lc_x_crd")
private int occrrncLcXCrd; // 위치 X 좌표
@JsonProperty("occrrnc_lc_y_crd")
private int occrrncLcYCrd; // 위치 Y 좌표
private Double loCrd; // 경도 좌표
private Double laCrd; // 위도 좌표
}
728x90
'My Proect > 중개폼 프로젝트 - Carblre' 카테고리의 다른 글
5일차 - DB 활용한 chart.js 차트 그리기(1) (2) | 2024.10.18 |
---|---|
4일차 - 공공 API 데이터 DB에 밀어 넣기 (1) | 2024.10.17 |
3일차 - 공공 API 데이터 가공 ~ing (0) | 2024.10.15 |
2일차 - 공공 API XML to JSON 변환하기 (2) | 2024.10.14 |
1일차 - 공공 API 'POSTMAN' 으로 'JSON 형식' 확인하기 (2) | 2024.10.11 |