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

查看:822
本文介绍了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能够自动检测到自定义的序列化器/反序列化器,但是既然没有,我该如何迅速地显示我的自定义JSON格式,而不是自己检测到的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.

解决方案非常简单.写一个表示您的自定义序列化对象的类.然后只需使用

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天全站免登陆