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

时间:2016-01-25 13:51:18

标签: java json spring 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
}

现在我知道,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);
}

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

1 个答案:

答案 0 :(得分: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);
    }

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

这就是您要寻找的