8

I would like to know if there is any way of making SpringFox to not show all the fields of a certain entity that aren't required in the call to an specific endpoint.

For example:

Having the following entity:

public class Car {
    long id;
    String name;
    int wheels;
    String type;
    boolean canFly;
}

And the following endpoints:

@RequestMapping(method = RequestMethod.GET,
                produces = MediaType.APPLICATION_JSON_VALUE)
public Car get(@RequestParam(value = "carId", required = true) long projectId) {
    return carService.get(carId);
}

@RequestMapping(method = RequestMethod.POST,
                consumes = MediaType.APPLICATION_JSON_VALUE,
                produces = MediaType.APPLICATION_JSON_VALUE)
public Car create(@RequestBody Car car) {
    return carService.create(car);
}

@RequestMapping(method = RequestMethod.PUT,
                consumes = MediaType.APPLICATION_JSON_VALUE,
                produces = MediaType.APPLICATION_JSON_VALUE)
public Car update(@RequestBody Car car) {
    return carService.update(car);
}

The thing is that in the create Car endpoint only name and wheels are required, but in the documentation Swagger-ui shows all the fields as if they were required. I've already tried @JsonViews but Springfox does not handle them yet.

Is there any way to avoid this?

2 Answers 2

2

Use the @ApiModelProperty (from io.swagger.annotations)

  • With required you define whether the property is mandatory or optional.
  • With hidden you can hide the property in Swagger UI, however if it's set it is returned anyway.

For example:

public class Car {

    @ApiModelProperty(value = "id", required = true)
    long id;

    @ApiModelProperty(value = "wheels", required = true)
    int wheels;

    @ApiModelProperty(value = "name", hidden = true)
    String name;

    @ApiModelProperty(value = "type", hidden = true)
    String type;

    @ApiModelProperty(value = "canFly", hidden = true)
    boolean canFly;
}

Since you use the same model for request and response (with the example above) the attributes in the documentation for GET endpoint will be also hidden (keep that in mind). If you don't want such behaviour then use separate models.

2
  • 1
    Looks like for now there is no escape in using separate models. Will have to wait until new version of SpringFox releases then
    – RabidTunes
    Commented Feb 18, 2019 at 16:47
  • @RabidTunes so in 2023 do we have any solution to keep 1 model but hide its few fields for an endPoint but keep for the another endPoint? (Spring WebFlux) Commented Jun 16, 2023 at 18:04
0

Starting from the springfox-boot-starter 3.0.0

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>

you can do it very easily and efficiently by adding Jackson JsonProperty annotation on the field you need.

@JsonProperty(access = JsonProperty.Access.READ_ONLY)

E.g. you have a model class Client and you want both:

  • hide field id in the swagger for the requests (e.g. POST and PUT) but show it in the responses
  • do not accept field id in your controller (so that it always will have value 0 no matter what caller set) but return id value of the Client in the response.

You can achieve these by adding annotation on the id field

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class Client {
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private long id;
    private String name;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.