Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

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 Accepted Solution

Avatar

Correct answer by
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. 

View solution in original post

1 Reply

Avatar

Correct answer by
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.