使用springfox-swagger2:3.0.0
时,某次项目启动出现报错提示:
03-25 10:37:50.773 [main] ERROR s.d.spring.web.plugins.DocumentationPluginsBootstrapper - Unable to scan documentation context default
java.lang.NullPointerException: null
at springfox.documentation.schema.Example.equals(Example.java:131)
at java.base/java.util.Objects.equals(Objects.java:77)
at springfox.documentation.service.RequestParameter.equals(RequestParameter.java:132)
at java.base/java.util.HashMap.putVal(HashMap.java:630)
at java.base/java.util.HashMap.put(HashMap.java:607)
at java.base/java.util.HashSet.add(HashSet.java:220)
at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at springfox.documentation.spring.web.readers.operation.OperationParameterReader.apply(OperationParameterReader.java:93)
at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.operation(DocumentationPluginsManager.java:144)
at springfox.documentation.spring.web.readers.operation.ApiOperationReader.read(ApiOperationReader.java:72)
逐个接口排查,发现某个接口定义的参数对象里面的属性,使用了public修饰,并且使用@Data
生成了对应的getter、setter方法:
@Data
@ApiModel("demoRequestModel")
public DemoRequest{
@ApiModelProperty("testProperty")
public String a;
}
当把该属性设置为private后(或者去除@Data注解),问题解决。
为什么呢?
调试代码发现,在springfox.documentation.spring.web.readers.parameter.ModelAttributeParameterExpander
类中的168行,存在如下代码:
private List<ModelAttributeField> allModelAttributes(
Map<Method, PropertyDescriptor> propertyLookupByGetter,
Iterable<ResolvedMethod> getters,
Map<String, ResolvedField> fieldsByName,
AlternateTypeProvider alternateTypeProvider,
Collection<Class> ignorables) {
Stream<ModelAttributeField> modelAttributesFromGetters =
StreamSupport.stream(getters.spliterator(), false)
.filter(method -> !ignored(alternateTypeProvider, method, ignorables))
.map(toModelAttributeField(fieldsByName, propertyLookupByGetter, alternateTypeProvider));
Stream<ModelAttributeField> modelAttributesFromFields =
fieldsByName.values().stream()
.filter(ResolvedMember::isPublic)
.filter(ResolvedMember::isPublic)
.map(toModelAttributeField(alternateTypeProvider));
return Stream.concat(
modelAttributesFromFields,
modelAttributesFromGetters)
.collect(toList());
}
跟踪代码可知,swagger在生成接口参数记录对象时,会保留参数对象中存在getter方法的属性,和使用public修饰的属性,如果同时满足这两个条件,则会造成冲突。