Cannot coerce empty String ("") to 'com.xxx.HolidayResponse$Items' value (but could if coercion was enabled using 'CoercionConfig')
현재 공휴일 정보를 가져오기 위해서 공공 API 호출하고 있다.
final ResponseEntity<HolidayResponse> response = restTemplate.getForEntity(uri, HolidayResponse.class);
공공 API로부터 공휴일 데이터를 json
으로 받아올 때, 년월에 공휴일이 없을 경우 items
필드에 빈 문자열("") 데이터가 들어온다. Jackson은 이 빈 문자열("")을 items
로 바인딩할 수 없어 예외가 발생한다.
🗒️ 공휴일 공공 API URL
https://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?serviceKey=${serviceKey}&solYear=2025&solMonth=04&_type=json
{
"response": {
"header": {
"resultCode": "00",
"resultMsg": "NORMAL SERVICE."
},
"body": {
"items": "",
"numOfRows": 10,
"pageNo": 1,
"totalCount": 0
}
}
}
@JsonCreator
어노테이션을 사용해서 해결했다.
items
데이터를 가져올 때, 해당 값이 String(빈 문자열, "")이면 null로 반환하고 그렇지 않다면 Items 객체로 반환한다.
public record Body(
Items items // 공휴일 항목
) {
@JsonCreator
public Body(@JsonProperty("items") Object items) {
this(items instanceof Items ? (Items) items : null);
}
}