• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

springfox(swagger2) 不适用于 GsonHttpMessageConverterConfig

用户头像
it1352
帮助1

问题说明

我正在尝试构建的是一个 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?

正确答案

#1

自己解决了问题:

问题在于序列化这个类:

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfigib
系列文章
更多 icon
同类精品
更多 icon
继续加载