How to use Gson library | Community
Skip to main content
New Participant
October 16, 2015
Solved

How to use Gson library

  • October 16, 2015
  • 1 reply
  • 2109 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

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 reply

smacdonald2008
smacdonald2008Accepted solution
New Participant
October 16, 2015

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.