如何在 swagger 中使用 Google Gson 而不是 Spring 的默认 Jackson? [英] how to use Google Gson instead of the default Jackson of the Spring in swagger?

查看:60
本文介绍了如何在 swagger 中使用 Google Gson 而不是 Spring 的默认 Jackson?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中工作,我想使用 Swagger 记录它.是一个用 Spring Boot 实现的项目,我们使用库com.google.code.gson"而不是默认的 SpringBootJackson",但 Gson Swagger 不起作用.

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.

问题在于返回模式.

Gson 返回如下:

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

Jackson 返回如下:

Jackson return as follow:

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

有谁知道我怎样才能让 Gson 工作?

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

推荐答案

Spring Boot 默认使用 Jackson 来序列化和反序列化 REST API 中的请求和响应对象.如果您想使用 GSON 而不是 Jackson,那么您可以在 pom.xml 或 build.gradle 文件中添加 Gson 依赖项,并在 application.properties 文件中指定一个属性来告诉 Spring Boot 使用 Gson 作为您首选的 json 映射器.

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

这就是你需要做的!

这篇关于如何在 swagger 中使用 Google Gson 而不是 Spring 的默认 Jackson?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆