Springfox/Swagger中的自定义ResponseModel,用于返回ObjectNode

Bru*_*206 7 java spring json swagger springfox

我想使用Swagger为我的Spring Boot API提供API文档.我设法使Springfox 2.3.0工作,除了返回ObjectNode的控制器之外,一切都按预期工作.Swagger尝试将返回的类(ObjectNode)转换为JSON-Representation,结果如下:

{
"array": true,
"bigDecimal": true,
"bigInteger": true,
"binary": true,
"boolean": true,
"containerNode": true,
"double": true,
"float": true,
"floatingPointNumber": true,
"int": true,
"integralNumber": true,
"long": true,
"missingNode": true,
"nodeType": "ARRAY",
"null": true,
"number": true,
"object": true,
"pojo": true,
"short": true,
"textual": true,
"valueNode": true
}
Run Code Online (Sandbox Code Playgroud)

现在我知道,Swagger无法猜测我构建的JSON中包含哪些值,但我想以任何形式手动添加正确的ResponseModel.

The Controller looks something like this:
@ApiOperation(value = "NAME", notes="NOTES")
@RequestMapping(value = "", method = RequestMethod.GET, produces="application/json")
public ResponseEntity<ObjectNode> getComponentByIdentification(@ApiParam(name="customerid", required=true, value="") @RequestParam (required = true) String customerId)
        return new ResponseEntity<ObjectNode>(someService.getCustomer(customerId), HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

有没有什么办法可以为Swagger提供一个自定义的ResponseJSON,它在文档中显示为Model Schema?

Tan*_*lly 0

我们可以使用 swagger springfox 注释在 swagger api 文档中给出自定义请求和响应模型,如下所示

@PostMapping
@ApiOperation(value = "Create Article", response = ArticleDTO.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "Article DTO", value = "article", required = true, dataType = "com.example.ArticleDTO", paramType = "body")})
    public Article create(@ApiIgnore Article article) {
        return articleRepository.save(article);
    }
Run Code Online (Sandbox Code Playgroud)

这里的请求和响应是 ArticleDTO,但将转换为 Jackson 的 Article,但文档将显示 ArticleDTO.java 中的内容

这是你想要的