7

I'd like to use Swagger to provide an API-Documentation for my Spring Boot API. I managed to get Springfox 2.3.0 to work and everything works as expected except for the controllers returning an ObjectNode. Swagger tries 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 can't guess which values are contained in the JSON I build, but I'd 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 any way I can provide a custom ResponseJSON to Swagger which is shown in the documentation as Model Schema?

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
Bruce206
  • 71
  • 4

1 Answers1

0

We can give custom request and response model in 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 = "com.example.ArticleDTO", paramType = "body")})
    public Article create(@ApiIgnore Article article) {
        return articleRepository.save(article);
    }

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

Is this what you are looking for

Tan mally
  • 572
  • 1
  • 5
  • 11