springfox(swagger2)不能使用GsonHttpMessageConverterConfig

我正在尝试构建的是Spring-Boot(v1.2.3)应用程序,并将Rest API与SpringFox(swagger2)v2.0.0

我的Swagger弹簧配置

@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,我这样做:

 @Configuration public class GsonHttpMessageConverterConfig { @Bean public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) { GsonHttpMessageConverter converter = new GsonHttpMessageConverter(); converter.setGson(gson); return converter; } } 

麻烦的是,如果使用GsonHttpMessageConverter ,swagger v2会生成一个错误的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带有值前缀,真正的JSON成为一个转义字符串。

这里是如何,如果不使用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 ... 

有没有解决方案来创建一个没有价值和转义的正确的招摇的JSON?

自己解决了这个问题:

问题在于序列化这个类:

 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配置:

适配器:

 public class SpringfoxJsonToGsonAdapter implements JsonSerializer { @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(); } } 

进入类似的问题,但发现一个不同的解决方案,也是使用上面提到的序列化器。

我们定义一个Bean来自动装载Gson对象。 为了解决Swagger的问题,还需要为Json类添加“registerTypeAdapter”。

 @Configuration public class GsonConfiguration { @Bean public Gson gson() { return new GsonBuilder().registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter()).create(); } } 

SpringfoxJsonToGsonAdapter的内容与上面相同,只是为了完整。

 public class SpringfoxJsonToGsonAdapter implements JsonSerializer { @Override public JsonElement serialize(Json json, Type type, JsonSerializationContext context) { final JsonParser parser = new JsonParser(); return parser.parse(json.value()); } } 

对于使用Gson对象只是做这样的事情:

 @Component public class Foobar { @Autowired Gson gson; @Autowired public Foobar() { // ... some constructor work ... } public void someMethod() { System.out.println(gson.toJson(...)); // Fill in some object ;-) } } 

这是Oleg Majewski为SpringFox + Gson问题翻译成Kotlin的解决方案:

 internal class SpringfoxJsonToGsonAdapter : JsonSerializer { override fun serialize(json: Json, type: Type, context: JsonSerializationContext): JsonElement = JsonParser().parse(json.value()) } @Configuration open class GsonHttpMessageConverterConfig { @Bean open fun gsonHttpMessageConverter(): GsonHttpMessageConverter { val converter = GsonHttpMessageConverter() converter.gson = gson() return converter } private fun gson(): Gson = GsonBuilder() .registerTypeAdapter(Json::class.java, SpringfoxJsonToGsonAdapter()) .create() }