raok1997
raok1997

Reputation: 404

spring-mvc swagger how to hide model property in swagger ui?

We are using SwaggerSpringMvcPlugin to generate swagger documentation as shown below.

@Bean
    public SwaggerSpringMvcPlugin swaggerSpringMvcPlugin(SpringSwaggerConfig springSwaggerConfig) {
        log.debug("Starting Swagger");
        StopWatch watch = new StopWatch();
        watch.start();
        SwaggerSpringMvcPlugin swaggerSpringMvcPlugin = new SwaggerSpringMvcPlugin(springSwaggerConfig)
            .apiInfo(apiInfo())
            .genericModelSubstitutes(ResponseEntity.class)
            .includePatterns(DEFAULT_INCLUDE_PATTERN);

        swaggerSpringMvcPlugin.build();
        watch.stop();
        log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
        return swaggerSpringMvcPlugin;
    }

I need to exclude some of the model properties as shown below. I tried using @ApiModelProperty(access="hidden", hidden=true), but they are not excluded. Note that all the propeties of MyModel are displayed including status field. Any help appreciated.

public class MyModel implements Serializable {
....

    @ApiModelProperty(access="hidden", hidden=true)
    @Column(name = "status")
    private String status;
...
}

Upvotes: 2

Views: 8166

Answers (2)

Roshan Wijendrs
Roshan Wijendrs

Reputation: 11

You can simply use

public class MyModel implements Serializable {
    @ApiModelProperty(hidden=true)
    private String status;
}

Upvotes: 1

Dilip Krishnan
Dilip Krishnan

Reputation: 5487

Assuming that you dont want to show this property or allow it to be editable via serialization, I'd say just adding the

@JsonIgnore 

Jackson2 annotation on your bean property will tell the model generation to excluded the property from being generated.

Also, keep in mind that, the annotation needs to be placed on the beans on getters or fields depending on how the ObjectMapper serialization/deserialization is configured.

If you merely want to hide the field from the swagger ui and allow modification/serialization of that field, then its not currently possible. However in the next version of the library, we plan to make this possible

Upvotes: 0

Related Questions