Custom ResponseModel in Springfox / Swagger to return ObjectNode

I would like to use Swagger to provide API documentation for my Spring boot API. I managed to get Springfox 2.3.0 to work, and everything works as expected, with the exception of controllers that return ObjectNode. Swagger is trying to convert the returned class (ObjectNode) to a JSON representation, and the result is this:

{ "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 } 

Now I know that Swagger cannot guess what values ​​are contained in the JSON I build, but I would like to manually add the correct ResponseModel in any form.

 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); } 

Is there a way to provide a custom ResponseJSON for Swagger, which is shown in the documentation as a model outline?

+7
source share
1 answer

We can provide a custom request and response model in the Swagger API documentation using Swagger Springfox annotations, as shown below

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

The request and response here is ArticleDTO, but it will be converted by Jackson to Article, but the documentation will show what is inside ArticleDTO.java.

This is what you are looking for

0
source

Source: https://habr.com/ru/post/1241418/


All Articles