答案 0 :(得分:2)
也许自定义的属性构建器插件可以提供帮助,尝试将allowEmptyValue
设置为null
。
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiModelProperty;
import springfox.documentation.builders.ModelPropertyBuilder;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
@Component
public class CustomizedModelPropertyBuilderPlugin implements ModelPropertyBuilderPlugin {
@Override
public boolean supports(final DocumentationType arg0) {
return true;
}
@Override
public void apply(final ModelPropertyContext context) {
final ModelPropertyBuilder builder = context.getBuilder();
final Optional<BeanPropertyDefinition> beanPropDef = context.getBeanPropertyDefinition();
if (!beanPropDef.isPresent()) {
return;
}
final BeanPropertyDefinition beanDef = beanPropDef.get();
final AnnotatedMethod method = beanDef.getGetter();
if (method == null) {
return;
}
final ApiModelProperty apiModelProperty = method.getAnnotation(ApiModelProperty.class);
if (apiModelProperty == null) {
return;
}
builder.allowEmptyValue(null);
}
}