Skip to content

SPR-9488 Add GsonHttpMessageConverter #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

royclarkson
Copy link
Member

This commit adds support to read and write JSON using the Google Gson
library. GsonHttpMessageConverter offers default Gson configuration, but
can be customized by using GsonFactoryBean. GsonFactoryBean includes
several convenience properties for configuring the internal GsonBuilder
and the resulting Gson object.

By default Gson converts byte arrays to JSON arrays instead of a Base64
encoded string. GsonBase64ByteArrayJsonTypeAdapter provides support to
read and write Base64 encoded byte arrays, and can be enabled in
GsonFactoryBean.

RestTemplate will enable GsonHttpMessageConverter only if Jackson 2 is
not found on the class path, because by default GsonHttpMessageConverter
supports the same media types as Jackson.

Issue: SPR-9488

public GsonHttpMessageConverter(GsonBuilder gsonBuilder) {
super(new MediaType("application", "json", DEFAULT_CHARSET),
new MediaType("application", "*+json", DEFAULT_CHARSET));
Assert.notNull(gsonBuilder, "'gson' must not be null");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it should be 'gsonBuilder' must not be null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected this in the updated PR. Thanks for reviewing!

@royclarkson
Copy link
Member Author

rebased against the latest changes from master and fixed merge issues

@Override
public JsonElement serialize(byte[] src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(new String(Base64.getEncoder().encode(src), DEFAULT_CHARSET));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realized I used the Java 8 Base64 encoder. This needs to be reworked to remove the Java 8 dependency.

@royclarkson
Copy link
Member Author

rebased against master and fixed merge issues

@royclarkson
Copy link
Member Author

Removed the dependency on Java 8 Base64 in favor of commons-codec.

@royclarkson
Copy link
Member Author

Added GsonFactoryBean, which migrates many of the convenience properties away from GsonHttpMessageConverter. Updated commit and PR description. Rebased against master and fixed merge issues. cc: @rstoyanchev

This commit adds support to read and write JSON using the Google Gson
library. GsonHttpMessageConverter offers default Gson configuration, but
can be customized by using GsonFactoryBean. GsonFactoryBean includes
several convenience properties for configuring the internal GsonBuilder
and the resulting Gson object.

By default Gson converts byte arrays to JSON arrays instead of a Base64
encoded string. GsonBase64ByteArrayJsonTypeAdapter provides support to
read and write Base64 encoded byte arrays, and can be enabled in
GsonFactoryBean.

RestTemplate will enable GsonHttpMessageConverter only if Jackson 2 is
not found on the class path, because by default GsonHttpMessageConverter
supports the same media types as Jackson.

Issue: SPR-9488
@rstoyanchev
Copy link
Contributor

Thanks Roy, this is now merged into master. I only added the converter to the MVC config much like it was already done in the RestTemplate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants