我在springboot项目上使用swagger。
问题出现在端点上,该端点返回具有MonetaryAmount列表的实体,即(List<MonetaryAmount> rates
)
Swagger-ui显示此类端点的错误数据:
{
"rates": [
null
]
}
但是我希望将每个MonetaryAmount值看作Double和String对。像这样的Smth:
{
"rates": [
{"currency":"EUR", "rate": 12.23}
]
}
我尝试使用directModelSubstitute但似乎它不适用于List。
这是我的模特:
public class CurrencyRatesResponse implements Serializable {
private List<MonetaryAmount> rates;
public CurrencyRatesResponse() {
}
public CurrencyRatesResponse(List<MonetaryAmount> rates) {
this.rates = rates;
}
public List<MonetaryAmount> getRates() {
return rates;
}
public void setRates(List<MonetaryAmount> rates) {
this.rates = rates;
}
}
这是我的终点
的一个例子@RequestMapping(path = "/public/rates", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public CurrencyRatesResponse getRates() {...}
生成的招摇JSON:
"/public/rates":{
"get":{
"tags":[
"rate-endpoint"
],
"summary":"getRates",
"operationId":"getRatesUsingGET",
"produces":[
"application/json;charset=UTF-8"
],
"parameters":[
{
....
}
],
"responses":{
"200":{
"description":"OK",
"schema":{
"$ref":"#/definitions/CurrencyRatesResponse"
}
},
"401":{
"description":"Unauthorized"
},
"403":{
"description":"Forbidden"
},
"404":{
"description":"Not Found"
}
}
}
}
} ....
"CurrencyRatesResponse":{
"type":"object",
"properties":{
"rates":{
"type":"array",
"items":{
"$ref":"#/definitions/MonetaryAmount"
}
}
},
"title":"CurrencyRatesResponse"
},
答案 0 :(得分:4)
实现这一目标有几个步骤。
首先,我们需要正确的objectMapper,以便MonetaryAmount
映射到具有两个字段currency
和rate
的正确对象。为此,我们需要为springfox的objectMapper添加自定义模块。
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.zalando.jackson.datatype.money.MoneyModule;
import springfox.documentation.schema.configuration.ObjectMapperConfigured;
@Component
public class SwaggerJacksonModule implements ApplicationListener<ObjectMapperConfigured> {
@Override
public void onApplicationEvent(ObjectMapperConfigured event) {
event.getObjectMapper().registerModule(new MoneyModule());
}
}
说明:我们在springfox的ObjectMapperConfigured
事件中添加一个监听器,每当触发事件时,我们只需获取映射器并注册我们的模块。我使用Zalando's Jackson数据类型资金库,这样我就不会自己编写。
下一步是处理实际的Swagger表示。让我们创建自己的表示对象MonetaryWrapper
,我们将用它来代替MonetaryAmount
。
public class MonetaryWrapper {
private BigDecimal amount;
private String currency;
// getters and setters (getters are important)
}
为了使单个对象表示正确,它足以将.directModelSubstitute(MonetaryAmount.class, MonetaryWrapper.class)
添加到Docket
对象。
但是收藏并不那么简单。例如,在这种情况下列出。我们需要将特殊AlternateTypeRule
对象添加到Docket
对象中,以便正确显示列表。
有关Springfox&#39; s documentation
中有关此主题的更多详细信息这样的事情:
TypeResolver resolver = new TypeResolver();
AlternateTypeRule monetaryAmountListRule =
newRule(resolver.resolve(List.class, MonetaryAmount.class),
resolver.resolve(List.class, MonetaryWrapper.class))
最后我们的Docket
bean看起来像这样:
@Bean
public Docket productApi() {
TypeResolver resolver = new TypeResolver();
AlternateTypeRule monetaryAmountListRule =
newRule(resolver.resolve(List.class, MonetaryAmount.class),
resolver.resolve(List.class, MonetaryWrapper.class));
return new Docket(DocumentationType.SWAGGER_2)
.alternateTypeRules(monetaryAmountListRule)
.directModelSubstitute(MonetaryAmount.class, MonetaryWrapper.class)
.select().apis(RequestHandlerSelectors.basePackage("com.swagger.test"))
.paths(PathSelectors.regex("/public.*")).build();
}
这里有一些演示截图:
执行请求时演示:
演示仅用于对象描述: