我有一个用@ApiParam
注释的方法,如下所示:
@RestController
@RequestMapping({LinksBuilder.BASE_URL})
@Api(tags = "Stuff Initiation", description="Stuff Initiation Service")
public class StuffResource {
@ApiOperation(value = "some description", tags = "Stuff Initiation")
@PostMapping(value = "/{stuffProduct}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Stuff InitiationResponse> postInitiateStuff (
@ApiParam(required=true,value="Stuff initiation payload")
@Valid @RequestBody Stuff Initiation stuffInitiation,
@ApiParam(name="stuffProduct", required= true, allowableValues="productStuff1,productStuff2,productStuff3")
@PathVariable String stuffProduct) throws StuffServiceException { ... }
...
}
问题是,由springfox(2.9.2)生成的swagger文档具有"allowEmptyValue":false
,而swagger标准在路径参数上不允许这样做。
为了解决这个问题,我实现了一个类似于springfox hide allowEmptyValue when field annotated with @ApiModelProperty的解决方案:
package com.example.config;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiParam;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ParameterBuilderPlugin;
import springfox.documentation.spi.service.contexts.ParameterContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 100)
public class CustomizedParameterBuilderPlugin implements ParameterBuilderPlugin {
@Override
public boolean supports(final DocumentationType arg0) {
return true;
}
@Override
public void apply(ParameterContext context) {
//Optional<ApiModelProperty> annotation = empty();
Optional<ApiParam> apiParam = context.resolvedMethodParameter().findAnnotation(ApiParam.class);
if (apiParam.isPresent()) {
//apiParam.get().allowEmptyValue();
context.parameterBuilder().allowEmptyValue(null);
System.err.println(apiParam.get().name() + "\t" + apiParam.get().type());
}
}
}
我得到了正确的元素,但是显然context.parameterBuilder().allowEmptyValue(null);
的设置不起作用...元素仍然生成了
我知道根本原因是known bug, and is set as status fixed,但我无法使用3.0.0-SNAPSHOT