springfox(swagger2)不能与GsonHttpMessageConverterConfig一起使用 [英] springfox(swagger2) does not work with GsonHttpMessageConverterConfig

查看:1039
本文介绍了springfox(swagger2)不能与GsonHttpMessageConverterConfig一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Spring-Boot(v1.2.3)应用程序,并使用SpringFox(swagger2)v2.0.0公开我的Rest API。

我的Swagger Spring配置




$ b

  @ 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 GsonHttpMessageConverterConfig {

@Bean
public
converter.setGson(gson);
返回转换器;




$ b $ p
$ b

麻烦的是如果使用 GsonHttpMessageConverter ,swagger v2生成错误的json:
$ b

  {
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:
...


$ b $如果不使用 GsonHttpMessageConverter,那么它应该是怎么样的:

  {
swagger:2.0 ,
info:{
descri Lorem Ipsum仅仅是印刷和排版行业的虚拟文本,自从16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一台未知的打印机采用了一种类型的厨房,并将其炒到了
...

是否有解决方案来创建没有值和转义的正确的Swagger 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(){
返回值;






$ b

正确地序列化它我实现了一个SpringfoxJsonToGsonAdapter并添加它到我的gson配置:

适配器:

  public class SpringfoxJsonToGsonAdapter实现了JsonSerializer< Json> {

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


$ / code>

gson config:

@Configuration
public class GsonHttpMessageConverterConfig {

$Be
public GsonHttpMessageConverter gsonHttpMessageConverter (){
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setGson(gson());
返回转换器;


private Gson gson(){
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Json.class,new SpringfoxJsonToGsonAdapter());
return builder.create();
}
}


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

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");
    }

}

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;
    }
}

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:
...

the JSON is prefixed with value and the real JSON becomes an escaped string.

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 
...

Is there a solution to create a correct swagger JSON without value and escaping?

解决方案

solved the issue by myself:

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;
  }
}

to serialize it correct I implemented a SpringfoxJsonToGsonAdapter and added it to my gson config:

adapter:

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 config:

@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屋!

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