반응형
상황
webclient를 이용하여 API를 호출하고 응답을 Map 형태로 반환함
- 처음 에러 발생한 코드
public Map getResponse(Map params) {
try {
Map result = this.webClient.post()
.uri("{{api 호출 url}}")
.body(BodyInserters.fromValue(params))
.retrieve()
.bodyToMono(Map.class)
.block();
return result;
} catch (Exception e) {
log.error("[getResponse] Cause: {}", e.getMessage());
return null;
}
}
이때 api 호출 시 "Non-standard token 'NaN': enable JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS to allow" 에러가 발생하고 result 는 null이었다.
- 수정한 코드
public Map getResponse(Map params) {
try {
// response를 String 타입으로 받아옴
String result = this.webClient.post()
.uri("{{api 호출 url}}")
.body(BodyInserters.fromValue(params))
.retrieve()
.bodyToMono(String.class)
.block();
// result를 Map 타입으로 변환
ObjectMapper mapper = new ObjectMapper();
mapper.enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS.mappedFeature()); // 이 부분을 추가해줘야 에러 해결
Map response = new HashMap<>();
try {
response = mapper.readValue(result, Map.class);
} catch (IOException e) {
e.printStackTrace();
}
return response;
} catch (Exception e) {
log.error("[getResponse] Cause: {}", e.getMessage());
return null;
}
}
반응형