내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.

해결됨

How to use Gson library

Avatar

Level 4

Hi,
I'm trying to import and use Google Gson library in my servlet but when I try to compile the servlet I get these errors

com.google.gson.JsonElement cannot be resolved to a type com.google.gson.JsonParser cannot be resolved to a type com.google.gson.JsonObject cannot be resolved to a type

The Gson library is in OSGi and Active and it exports these packages

com.google.gson,version=2.2.4 com.google.gson.annotations,version=2.2.4 com.google.gson.internal,version=2.2.4 com.google.gson.internal.bind,version=2.2.4 com.google.gson.reflect,version=2.2.4 com.google.gson.stream,version=2.2.4

How can I use the library in my servlets? I need it to parse JSON strings.

Thanks for any help

1 채택된 해결책 개

Avatar

정확한 답변 작성자:
Level 10

For this use case -- you have to place the JSON JAR into an OSGi bundle and deploy to CQ.

We have a community article that walks you through this process. See

"Add the org.json.simple.JSONObject data type to Adobe CQ "

in this AEM community article:

http://scottsdigitalcommunity.blogspot.ca/2013/06/posting-form-data-to-adobe-cq-using.html

In this article -- notice that a sling servlet encodes data as JSON using this lib.

Build your Bundle as well  using Maven as specified in this article. Once you do that -- CQ will have no issues finding the libs.

One issue you may have encountered is the versions.  Make sure that the versions of the JSON packages exported in the bundle are the same versions you use. 

원본 게시물의 솔루션 보기

1 답변 개

Avatar

정확한 답변 작성자:
Level 10

For this use case -- you have to place the JSON JAR into an OSGi bundle and deploy to CQ.

We have a community article that walks you through this process. See

"Add the org.json.simple.JSONObject data type to Adobe CQ "

in this AEM community article:

http://scottsdigitalcommunity.blogspot.ca/2013/06/posting-form-data-to-adobe-cq-using.html

In this article -- notice that a sling servlet encodes data as JSON using this lib.

Build your Bundle as well  using Maven as specified in this article. Once you do that -- CQ will have no issues finding the libs.

One issue you may have encountered is the versions.  Make sure that the versions of the JSON packages exported in the bundle are the same versions you use.