Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringFox Swagger - Optional and Mandatory fields in model

I used SpringFox library for rest documentation of my spring boot app. When I click on model , all the elements are being returned as optional. Is there a way to display required elements as mandatory? Is there any additional configuration that needs to be added?

like image 594
Punter Vicky Avatar asked May 03 '17 22:05

Punter Vicky


People also ask

What is Springfox swagger?

Springfox is a set of Java libraries, that has evolved from the swagger-springmvc project. It automates the generation of specifications for JSON APIs, implemented with the Spring framework. Also, it provides libraries to integrate the Swagger UI to interact with APIs.

How does Springfox work?

Springfox works by examining an application, once, at runtime to infer API semantics based on spring configurations, class structure and various compile time java Annotations.

What is the use of @EnableSwagger2?

The @EnableSwagger2 annotation is used to enable the Swagger2 for your Spring Boot application. Next, create Docket Bean to configure Swagger2 for your Spring Boot application.

Where is Swagger UI in HTML?

This will be located at localhost:8080/swagger-ui. html. Swagger will display the API endpoints which you have configured it to generate documentation for.


2 Answers

Yes by default All the fields will be optional. To mark a field as required you can use following annotation.

@ApiModelProperty(required = true)

On the getter method of the field which should be required. This won't show the field as "mandatory". But the optional tag will be removed for this field in the documentation.

Hope this helps.

like image 110
Ganesh Avatar answered Sep 19 '22 13:09

Ganesh


Support for bean validation annotations was added, specifically for @NotNull, @Min, @Max, and @Size in Springfox v2.3.2.

You can place those annotations on any of your API models.

In order to use it add the springfox-bean-validators dependency:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-bean-validators</artifactId>
</dependency>

Add to your application's configuration class:

@Import({springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})

See: https://springfox.github.io/springfox/docs/current/#springfox-support-for-jsr-303

like image 25
etech Avatar answered Sep 22 '22 13:09

etech