springfox(swagger2) 不适用于 GsonHttpMessageConverterConfig [英] springfox(swagger2) does not work with GsonHttpMessageConverterConfig
问题描述
我正在尝试构建的是一个 spring-boot (v1.2.3) 应用程序并使用 SpringFox(swagger2) v2.0.0 公开我的 Rest API
What I am trying to build is a spring-boot (v1.2.3) application and expose my Rest API with SpringFox(swagger2) v2.0.0
我的 Swagger Spring 配置
my Swagger Spring config
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket myApi() {
return new Docket(DocumentationType.SWAGGER_2)
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping("/my-prj");
}
}
我需要使用 gson 将我的 pojo 转换为 json,我是这样做的:
I need to use gson to convert my pojo's to json, and I do it this way:
@Configuration
public class GsonHttpMessageConverterConfig {
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) {
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setGson(gson);
return converter;
}
}
麻烦的是如果使用GsonHttpMessageConverter
,swagger v2会生成错误的json:
The trouble is that if using GsonHttpMessageConverter
, swagger v2 generates a wrong json:
{
"value": "{"swagger":"2.0","info":{"description":"Api Documentation","version":"1.0","title":"Api Documentation","termsOfService":"urn:tos","contact":{"name":"Contact Email"},"license":{"name":"Apache 2.0","url":"http:
...
JSON 以 value 为前缀,真正的 JSON 成为转义字符串.
the JSON is prefixed with value and the real JSON becomes an escaped string.
如果不使用 GsonHttpMessageConverter
应该是这样的:
here is how it should be if not using GsonHttpMessageConverter
:
{
"swagger": "2.0",
"info": {
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a
...
有没有一种解决方案可以创建一个没有值和转义的正确的 swagger JSON?
Is there a solution to create a correct swagger JSON without value and escaping?
推荐答案
自己解决了问题:
问题在于序列化这个类:
the issue was with serializing this class:
package springfox.documentation.spring.web.json;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.annotation.JsonValue;
public class Json {
private final String value;
public Json(String value) {
this.value = value;
}
@JsonValue
@JsonRawValue
public String value() {
return value;
}
}
为了正确序列化它,我实现了一个 SpringfoxJsonToGsonAdapter 并将其添加到我的 gson 配置中:
to serialize it correct I implemented a SpringfoxJsonToGsonAdapter and added it to my gson config:
适配器:
public class SpringfoxJsonToGsonAdapter implements JsonSerializer<Json> {
@Override
public JsonElement serialize(Json json, Type type, JsonSerializationContext context) {
final JsonParser parser = new JsonParser();
return parser.parse(json.value());
}
}
gson 配置:
@Configuration
public class GsonHttpMessageConverterConfig {
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter() {
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setGson(gson());
return converter;
}
private Gson gson() {
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter());
return builder.create();
}
}
这篇关于springfox(swagger2) 不适用于 GsonHttpMessageConverterConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!