Spingfox 在为 swagger 生成 JSON 模型时无法识别自定义序列化程序 [英] Spingfox not recognizing custom serializer when generating JSON model for swagger

查看:48
本文介绍了Spingfox 在为 swagger 生成 JSON 模型时无法识别自定义序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 jhipster v2.23.1 应用程序使用自定义序列化器和反序列化器进行 JSON 解析,我在 JacksonConfiguration 中将其注册为模块.REST API 使用我的自定义映射按预期工作.

My jhipster v2.23.1 app uses custom serializers and deserializers for JSON parsing which I register as a module in JacksonConfiguration. The REST API works as expected using my custom mapping.

但是,自动生成的 Swagger 文档中显示的 JSON 不反映自定义映射.我希望 swagger 会自动检测自定义序列化器/反序列化器,但既然它没有,我怎样才能让 swagger 显示我的自定义 JSON 格式而不是它自己检测到的格式?

However, the JSON displayed in the auto-generated swagger documentation doesn't reflect the custom mapping. I was hoping swagger would detect custom serializers/deserializers automatically, but since it doesn't, how can I get swagger to show my custom JSON format instead of the one it detects on its own?

基于 http://springfox.github.io 上的 springfox 文档/springfox/docs/current/#configuring-springfox 我已经实现了接口:

Based on the springfox documentation at http://springfox.github.io/springfox/docs/current/#configuring-springfox i've implemented the interface:

ApplicationListener<ObjectMapperConfigured> 

在我的 SwaggerConfiguration bean 中.我可以看到 onApplicationEvent(ObjectMapperConfigured event) 方法被调用了两次.映射器第一次将按预期序列化我的对象,第二次则不会.如果我用映射器注册我的模块似乎也没有什么区别.我在这里使用的对象是一个联系人.

in my SwaggerConfiguration bean. I can see that the onApplicationEvent(ObjectMapperConfigured event) method is called twice. The first time the mapper will serialize my object as expected, the second time it will not. It also doesn't seem to make a difference if I register my module with the mapper or not. The object I'm working with here is a Contact.

@Override
public void onApplicationEvent(ObjectMapperConfigured event) {
    ObjectMapper mapper = event.getObjectMapper();

    // Custom serialization for Contact objects
    SimpleModule contactModule = new SimpleModule("Contact Module");
    contactModule.addSerializer(new ContactSerializer(Contact.class));
    contactModule.addDeserializer(Contact.class, new ContactDeserializer(Contact.class));

    mapper.registerModule(contactModule);

    // My custom object
    Contact c = new Contact();
    c.setCity("Springfield");
    c.setEmail("someone@gmail.com");

    String contactJsonStr = null;
    try {
        contactJsonStr = mapper.writeValueAsString(c);
    } catch(JsonProcessingException e) {
        e.printStackTrace();
    }
    System.out.println("Serialized Contact: " + contactJsonStr);
}

如何让 springfox 使用我的自定义序列化程序来构建我的招摇文档?还是我应该完全使用不同的方法?

How can I get springfox to use my custom serializer in order to build my swagger documentation? Or should I be using a different approach entirely?

推荐答案

嘿,我知道这是一个老问题,但我偶然发现了同样的问题并做了一些研究.

Hey I know this is an old question but i stumbled uppon the same problem and done a little research.

解决方案非常简单.编写一个代表您的自定义序列化对象的类.然后只需使用 directModelSubstitute 方法在您的 Docket 方法中使用序列化模型替换您的原始模型类.

The solution is quite simple. Write a class wich represents your custom serialized object. Then just use the directModelSubstitute method in your Docket method to substitute your original model class with the serialized model.

如果您的序列化程序执行这样的操作将 DateTime 序列化为 UNIX 时间(长)

If your serializer does something like this to serialise the DateTime into UNIX Time (Long)

public void serialize(final DateTime value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException, JsonProcessingException {
        long millis = value.getMillis();
        gen.writeNumber(millis);
}

只需将 .directModelSubstitute(DateTime.class, Long.class) 这一行添加到您的 Docket 定义中.

Just add .directModelSubstitute(DateTime.class, Long.class) this line to your Docket definition.

这篇关于Spingfox 在为 swagger 生成 JSON 模型时无法识别自定义序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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