Springfox Swagger生成创建奇数模式 [英] Springfox Swagger generation creates odd schema

查看:98
本文介绍了Springfox Swagger生成创建奇数模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Springfox-swagger为我的Spring Boot REST服务生成一个Swagger页面.作为我序列化为JSON的POJO的一部分,我使用了:

  • javax.money.MonetaryAmount
  • java.time.LocalDate

我看到的问题是,在Swagger中,响应的模型"和模型Shema"选项卡显示了这些字段的实现细节,即

LocalDate {
 chronology (IsoChronology, optional),
 dayOfMonth (integer, optional),
 dayOfWeek (string, optional) = ['MONDAY' or 'TUESDAY' or 'WEDNESDAY' or 'THURSDAY' or 'FRIDAY' or 'SATURDAY' or 'SUNDAY'],
 dayOfYear (integer, optional),
 era (Era, optional),
 leapYear (boolean, optional),
 month (string, optional) = ['JANUARY' or 'FEBRUARY' or 'MARCH' or 'APRIL' or 'MAY' or 'JUNE' or 'JULY' or 'AUGUST' or 'SEPTEMBER' or 'OCTOBER' or 'NOVEMBER' or 'DECEMBER'],
 monthValue (integer, optional),
 year (integer, optional)
}
IsoChronology {
  calendarType (string, optional),
  id (string, optional)
}
Era {
  value (integer, optional)
}

MonetaryAmount {
  context (MonetaryContext, optional),
  currency (CurrencyUnit, optional),
  factory (MonetaryAmountFactory«MonetaryAmount», optional),
  negative (boolean, optional),
  negativeOrZero (boolean, optional),
  number (NumberValue, optional),
  positive (boolean, optional),
  positiveOrZero (boolean, optional),
  zero (boolean, optional)
}
MonetaryContext {
  (...)
}
CurrencyUnit {
  (...)
}
CurrencyContext {
  (...)
}
MonetaryAmountFactory«MonetaryAmount» {
  (...)
}
NumberValue {
  (...)
}

如果这很重要,我正在使用javax.money.MonetaryAmount(JSR 354)的Moneta 1.1实现.

现在,这对于我想要的内容来说太冗长了,因此我实现了序列化程序,并且我的响应(和请求)按预期工作.但是,方案并未反映出这一点.序列化请求后,是否可以配置架构格式?

当我意识到此问题有很多可动部分时,我创建了一个证明概念项目,以全面展现该问题.

解决方案

基于 docket .directModelSubstitute(LocalDate.class, String.class) .directModelSubstitute(MonetaryAmount.class, Double.class)

有三种方法可以执行此操作,对于常规类型,可以通过directModelSubstitutes;对于单类型参数通用类型,可以通过genericModelSubstitutes;对于更复杂的通用类型用例,可以通过alternateTypeRules.

I am using Springfox-swagger to generate a Swagger page for my Spring Boot REST service. As a part of one of my POJOs that I serialise to JSON I use:

  • javax.money.MonetaryAmount
  • java.time.LocalDate

The issue I'm seeing is that in Swagger, the Model and Model Shema tabs for the response show the implementation details of these fields, namely

LocalDate {
 chronology (IsoChronology, optional),
 dayOfMonth (integer, optional),
 dayOfWeek (string, optional) = ['MONDAY' or 'TUESDAY' or 'WEDNESDAY' or 'THURSDAY' or 'FRIDAY' or 'SATURDAY' or 'SUNDAY'],
 dayOfYear (integer, optional),
 era (Era, optional),
 leapYear (boolean, optional),
 month (string, optional) = ['JANUARY' or 'FEBRUARY' or 'MARCH' or 'APRIL' or 'MAY' or 'JUNE' or 'JULY' or 'AUGUST' or 'SEPTEMBER' or 'OCTOBER' or 'NOVEMBER' or 'DECEMBER'],
 monthValue (integer, optional),
 year (integer, optional)
}
IsoChronology {
  calendarType (string, optional),
  id (string, optional)
}
Era {
  value (integer, optional)
}

and

MonetaryAmount {
  context (MonetaryContext, optional),
  currency (CurrencyUnit, optional),
  factory (MonetaryAmountFactory«MonetaryAmount», optional),
  negative (boolean, optional),
  negativeOrZero (boolean, optional),
  number (NumberValue, optional),
  positive (boolean, optional),
  positiveOrZero (boolean, optional),
  zero (boolean, optional)
}
MonetaryContext {
  (...)
}
CurrencyUnit {
  (...)
}
CurrencyContext {
  (...)
}
MonetaryAmountFactory«MonetaryAmount» {
  (...)
}
NumberValue {
  (...)
}

I am using the Moneta 1.1 implementation of javax.money.MonetaryAmount (JSR 354) if that matters.

Now, this is far to verbose for what I want, so I implemented a Serialiser, and my response (and requests) work as expected. However, the schemas are not reflecting this. Is there a way of configuring the format of the schemas after serialising the requests?

As I realise there are quite a few moving parts to this problem, I've created a proof of concept project to show the problem in its full glory.

解决方案

Based on your example looks like you're converting MonetaryAmount to double and LocalDate to String in your serializers.

Because springfox cannot introspect the jackson machinery (at least not yet :-)), we need to tell SpringFox what you're doing in your serializers. To do that, as described in the documentation in your docket you can provide a hint by providing alternate type rules.

docket
      .directModelSubstitute(LocalDate.class, String.class)
      .directModelSubstitute(MonetaryAmount.class, Double.class)

There are three ways to do this, either via directModelSubstitutes for regular types, genericModelSubstitutes for single type argument generic types and alternateTypeRules for more complex generic type use cases.

这篇关于Springfox Swagger生成创建奇数模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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