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?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With