我的jhipster v2.23.1应用程序使用自定义序列化器和反序列化器进行JSON解析,我在
然而,在自动生成的swagger文档中显示的JSON并不反映自定义映射。我希望swagger能够自动检测到自定义序列化器/反序列化器,但它没有这样做,那么我该如何让swagger显示我的自定义JSON格式,而不是它自己检测到的格式?
根据http://springfox.github.io/springfox/docs/current/#configuring-springfox上的springfox文档,我已经实现了接口。
在我的SwaggerConfiguration bean中,我可以看到
我该如何让Springfox使用我的自定义序列化器来构建Swagger文档?或者我应该采用完全不同的方法吗?
JacksonConfiguration
中注册为模块。使用我的自定义映射可以正常工作的REST API。然而,在自动生成的swagger文档中显示的JSON并不反映自定义映射。我希望swagger能够自动检测到自定义序列化器/反序列化器,但它没有这样做,那么我该如何让swagger显示我的自定义JSON格式,而不是它自己检测到的格式?
根据http://springfox.github.io/springfox/docs/current/#configuring-springfox上的springfox文档,我已经实现了接口。
ApplicationListener<ObjectMapperConfigured>
在我的SwaggerConfiguration bean中,我可以看到
onApplicationEvent(ObjectMapperConfigured event)
方法被调用了两次。第一次,映射器会按预期对我的对象进行序列化,第二次则不会。似乎注册模块与否也没有影响。我正在处理的对象是一个联系人(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使用我的自定义序列化器来构建Swagger文档?或者我应该采用完全不同的方法吗?