从 "allowableValues" 中的数据库传递值?
Passing values from database in "allowableValues"?
我正在使用带有 Java Spring 的 Swagger 版本 2。我已经声明了一个 属性 并且它工作正常并且它生成了一个我分配的值的下拉列表。
@ApiParam(value = "Pass any one Shuttle provider ID from the list", allowableValues = "1,2,3,4,10")
private Long hotelId;
现在,我需要一种方法来填充从我的数据库 allowableValues
传入的列表,因为它可能是随机列表以及大量数据。如何在此 allowableValues
?
中从数据库动态分配值列表
- 您需要在 SwaggerConfiguration class 中创建构造函数。
- @Autowire 服务并从数据库中提取您需要的数据
- 将此分配给
final
变量
- 将此
final
变量分配给注释中的 allowableValues
享受不高效api
private final String allowableValues;
public SwaggerConfiguration() {
List<YourEntitiy> list = someService.findAll();
//code to get every value you need and add create comma separated String
StringJoiner stringJoiner = new StringJoiner(",");
stringJoiner.add(list.get(0).getValue());
this.allowableValues = stringJoiner.toString();
}
@ApiParam(allowableValues = allowableValues)
但我认为从数据库中获取所有 ID 只是为了创建允许的值是个坏主意。只需在 api 方法中验证该 ID 是否存在 and/or 创建新的 api 以从数据库获取 ID,使用来自 Spring 数据项目的分页,例如 PageImpl<> javadocs
这个问题有点老了,我也遇到了同样的问题,所以想到在这里添加,这可能会对某些人有所帮助。
//对于ApiModelProperty
@ApiModelProperty(required = true, allowableValues = "dynamicEnum(AddressType)")
@JsonProperty("type")
private String type;
创建了一个实现 ModelPropertyBuilderPlugin 的组件
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1)
public class ApiModelPropertyPropertyBuilderCustom implements ModelPropertyBuilderPlugin {
private final DescriptionResolver descriptions;
@Autowired
public ApiModelPropertyPropertyBuilderCustom(DescriptionResolver descriptions) {
this.descriptions = descriptions;
}
public void apply(ModelPropertyContext context) {
try {
AllowableListValues allowableListValues = (AllowableListValues) FieldUtils.readField(context.getBuilder(),
"allowableValues", true);
if(allowableListValues!=null) {
String allowableValuesString = allowableListValues.getValues().get(0);
if (allowableValuesString.contains("dynamicEnum")) {
String yourOwnStringOrDatabaseTable = allowableValuesString.substring(allowableValuesString.indexOf("(")+1, allowableValuesString.indexOf(")"));
//Logic to Generate dynamic values and create a list out of it and then create AllowableListValues object
context.getBuilder().allowableValues(allowableValues);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
}
与 ApiParam 类似,我们可以创建将实现 ParameterBuilderPlugin 的组件
@Override
public void apply(ParameterContext context) {
@SuppressWarnings("Guava") final Optional<ApiParam> apiParam =
context.resolvedMethodParameter().findAnnotation(ApiParam.class);
if (apiParam.isPresent()) {
final String allowableValuesString = apiParam.get().allowableValues();
//Your logic here
context.parameterBuilder().allowableValues(allowableValues);
}
}
我正在使用带有 Java Spring 的 Swagger 版本 2。我已经声明了一个 属性 并且它工作正常并且它生成了一个我分配的值的下拉列表。
@ApiParam(value = "Pass any one Shuttle provider ID from the list", allowableValues = "1,2,3,4,10")
private Long hotelId;
现在,我需要一种方法来填充从我的数据库 allowableValues
传入的列表,因为它可能是随机列表以及大量数据。如何在此 allowableValues
?
- 您需要在 SwaggerConfiguration class 中创建构造函数。
- @Autowire 服务并从数据库中提取您需要的数据
- 将此分配给
final
变量 - 将此
final
变量分配给注释中的allowableValues
享受不高效api
private final String allowableValues; public SwaggerConfiguration() { List<YourEntitiy> list = someService.findAll(); //code to get every value you need and add create comma separated String StringJoiner stringJoiner = new StringJoiner(","); stringJoiner.add(list.get(0).getValue()); this.allowableValues = stringJoiner.toString(); } @ApiParam(allowableValues = allowableValues)
但我认为从数据库中获取所有 ID 只是为了创建允许的值是个坏主意。只需在 api 方法中验证该 ID 是否存在 and/or 创建新的 api 以从数据库获取 ID,使用来自 Spring 数据项目的分页,例如 PageImpl<> javadocs
这个问题有点老了,我也遇到了同样的问题,所以想到在这里添加,这可能会对某些人有所帮助。
//对于ApiModelProperty
@ApiModelProperty(required = true, allowableValues = "dynamicEnum(AddressType)")
@JsonProperty("type")
private String type;
创建了一个实现 ModelPropertyBuilderPlugin 的组件
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1)
public class ApiModelPropertyPropertyBuilderCustom implements ModelPropertyBuilderPlugin {
private final DescriptionResolver descriptions;
@Autowired
public ApiModelPropertyPropertyBuilderCustom(DescriptionResolver descriptions) {
this.descriptions = descriptions;
}
public void apply(ModelPropertyContext context) {
try {
AllowableListValues allowableListValues = (AllowableListValues) FieldUtils.readField(context.getBuilder(),
"allowableValues", true);
if(allowableListValues!=null) {
String allowableValuesString = allowableListValues.getValues().get(0);
if (allowableValuesString.contains("dynamicEnum")) {
String yourOwnStringOrDatabaseTable = allowableValuesString.substring(allowableValuesString.indexOf("(")+1, allowableValuesString.indexOf(")"));
//Logic to Generate dynamic values and create a list out of it and then create AllowableListValues object
context.getBuilder().allowableValues(allowableValues);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
}
与 ApiParam 类似,我们可以创建将实现 ParameterBuilderPlugin 的组件
@Override
public void apply(ParameterContext context) {
@SuppressWarnings("Guava") final Optional<ApiParam> apiParam =
context.resolvedMethodParameter().findAnnotation(ApiParam.class);
if (apiParam.isPresent()) {
final String allowableValuesString = apiParam.get().allowableValues();
//Your logic here
context.parameterBuilder().allowableValues(allowableValues);
}
}