swagger不陌生,API接口利器,本次要解决的问题是:我们知道前端在接收Long类型的属性时会出现精度问题,一般我们会在序列化的时候将Long类型的数字转换成String但是swagger的API文档中的类型还是Long,我们要解决的就是这个问题
不知道swagger怎么配置得可以看之前的文章:springboot集成swagger并更换主题_swagger 主题-CSDN博客
修改之前
可以看一下修改之前的API文档
如何配置
想要配置成String类型的其实也很简单,只需要在创建Docket时添加规则即可
@Bean
public Docket createRestApi(TypeResolver typeResolver) {
return new Docket(DocumentationType.OAS_30)
.globalRequestParameters(
Lists.newArrayList(
new RequestParameterBuilder()
.name(CommonConstant.JWT_DEFAULT_TOKEN_NAME)
.description("Token Request Header")
.in(ParameterType.HEADER)
.query(b -> b.allowEmptyValue(true).defaultValue(""))
.build(),
new RequestParameterBuilder()
.name(CommonConstant.SWAGGER_USER_NAME)
.description("用户名")
.in(ParameterType.HEADER)
.query(b -> b.allowEmptyValue(true).defaultValue("admin"))
.build(),
new RequestParameterBuilder()
.name(CommonConstant.SWAGGER_USER_PWD)
.description("密码")
.in(ParameterType.HEADER)
.query(b -> b.allowEmptyValue(true).defaultValue("1"))
.build()))
.apiInfo(apiInfo())
.select()
.apis(createBasePackage(basePackage))
.paths(PathSelectors.any())
.build()
.alternateTypeRules(
newRule(Long.class, String.class),
newRule(Integer.class, String.class),
newRule(int.class, String.class),
newRule(long.class, String.class),
newRule(BigDecimal.class, String.class),
newRule(Float.class, String.class),
newRule(float.class, String.class),
newRule(Double.class, String.class),
newRule(double.class, String.class),
newRule(Date.class, String.class),
newRule(
typeResolver.resolve(List.class, Integer.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, Long.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, BigDecimal.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, Float.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, float.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, Double.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, double.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, Boolean.class),
typeResolver.resolve(List.class, String.class)
),
newRule(
typeResolver.resolve(List.class, Date.class),
typeResolver.resolve(List.class, String.class)
));
}
这里是添加的匹配规则,将所有数字类型的数字或数组全部都转换成了字符串类型的