본문 바로가기

클라우드

Naver Cloud, Cloud Functions 에러 GSON parse

반응형
{
	"error":"An error has occured while invoking the action (see logs for details): java.lang.NoSuchMethodError: com/google/gson/JsonParser.parseReader(Ljava/io/Reader;)Lcom/google/gson/JsonElement; (loaded from file:/javaAction/build/libs/javaAction-all.jar by sun.misc.Launcher$AppClassLoader@75f1e6d) called from class com.saltware.enwork.batch.DailyAnnualCalcAPI (loaded from file:/tmp/useraction2847194860206102214.jar by org.apache.openwhisk.runtime.java.action.JarLoader@91766505)."
}

 

An error has occured while invoking the action (see logs for details): java.lang.NoSuchMethodError: com/google/gson/JsonParser.parseString(Ljava/io/Reader;)Lcom/google/gson/JsonElement; (loaded from file:/javaAction/build/libs/javaAction-all.jar by sun.misc.Launcher$AppClassLoader@75f1e6d) called from class com.saltware.enwork.batch.DailyAnnualCalcAPI (loaded from file:/tmp/useraction2847194860206102214.jar by org.apache.openwhisk.runtime.java.action.JarLoader@91766505)

 

네이버 클라우드 플랫폼의 Cloud functions 서비스를 사용하여 API를 호출하고 결과를 JSON으로 받아오던 중 위와같은 에러가 발생했다.

1. 결론

return JsonParser.parse(responseBodyString).getAsJsonObject();

대신 아래의 코드를 사용한다.

JsonParser parser = new JsonParser();
return parser.parse(responseBodyString).getAsJsonObject();

 

2. 원인

Cloud functions에서 classpath에 포함시켜주는 Gson 라이브러리의 버전이 얼마인지는 모르겠으나 내가 maven dependency에 추가한 버전보다 낮아서 인터페이스가 달랐다. 내 local환경에서는 문제없이 돌아가던 코드가 Cloud functions환경에서는 해당 메소드가 없다고 에러가 난 것이다.

 

Cloud functions의 runtime을 JAVA로 설정하면 main class와 main method명을 함께 기입하도록 되어있다. 정해진 schedule에 따라서 해당 method를 실행시켜 주는데 return type과 argument type이 Gson의 JsonObject이다.

 

자세한 내용은 JAVA의 method signature에 대해 알아보도록 하자.

 

아무튼 Cloud functions service에서는 실행매개변수를 Gson의 JsonObject로 넘겨준다. 이 말은 내가 업로드한 JAR파일이 실행되는데 있어서 classpath에 Gson 및 하위 라이브러리들을 넣어준다는 뜻이다.

 

원래 Jackson형님의 팬인 나는 jackson databind의 ObjectMapper를 즐겨쓰는 편인데, 네이버에서 넣어준다고 하니 Gson을 사용했다가 뒤통수를 맞았다.

반응형