how to use Google Gson instead of the default Jack

2019-07-24 21:15发布

问题:

I'm working in a project and I would like to documentation it using Swagger. Is a project implemented with Spring Boot and we use the library "com.google.code.gson" instead of default of SpringBoot "Jackson", but with Gson Swagger not works.

The problem is with the pattern of return.

Gson return as follow:

{"value":"{\"swagger\":\"2.0\",\"info\":{\"description\":

Jackson return as follow:

{"swagger":"2.0","info":{"description"

Does anyone have any idea how can I make Gson works?

回答1:

Spring Boot uses Jackson by default in order to serialize and deserialize request and response objects in the REST APIs. If you want to use GSON instead of Jackson then you can add Gson dependency in your pom.xml or build.gradle file and specify a property in the application.properties file to tell Spring Boot to use Gson as your preferred json mapper.

# Preferred JSON mapper to use for HTTP message conversion.
spring.http.converters.preferred-json-mapper=gson

that's all you need to do!