0

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.

Gregg
  • 34,973
  • 19
  • 109
  • 214

1 Answers1

0

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.

Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65
  • Yea, that's not what I need. I figured it out yesterday using `@JsonView`. I'll be posting my own answer in a bit. Thanks for the suggestion though. – Gregg May 06 '16 at 13:00
  • 1
    What did you do about this @Gregg ? JsonView doesn't seem to work. – NeilS Jan 04 '17 at 11:52
  • in swagger 3 should use ```@Schema(hidden = true)``` instead – Alex Jul 20 '22 at 16:00