0
votes

I'm using Swagger2 with Springfox and Spring Boot. I have an endpoint defined like so:

@ApiOperation(value = "save", nickname = "Save Store")
@ApiResponses(value = {
        @ApiResponse(code = 201, message = "Created"),
        @ApiResponse(code = 401, message = "Unauthorized"),
        @ApiResponse(code = 403, message = "Forbidden"),
        @ApiResponse(code = 500, message = "Failure", response = ErrorResource.class)})
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public void save(@Valid @RequestBody Store store, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) {
    if (bindingResult.hasErrors()) {
        throw new InvalidRequestException("Invalid Store", bindingResult);
    }

    this.storeService.save(store);
    response.setHeader("Location", request.getRequestURL().append("/").append(store.getId()).toString());
}

The generated API docs are showing the id of Store in the Model Schema. Technically, when creating a Store the JSON should not contain the id. I'm trying to figure out how to tell Swagger/Springfox to ignore the id but only for this endpoint.

1

1 Answers

0
votes

You can hide a field from a model by annotating the property of the class with @ApiModelProperty and setting its hidden property to true.

import io.swagger.annotations.ApiModelProperty;

public class Store {

    @ApiModelProperty(hidden = true)
    private Long id;

}

Unfortunately, by doing so, you will hide the id field on every endpoint which uses the Store class as an input. Showing the field for another endpoint would require a separate class.