I followed this tutorial https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/ to generate a swagger documentation. It's working but when I try to add some validation in my bean I don't find the information in the documentation:
@ApiOperation(value = "Creates a product",
notes="Populates a product instance bla bla")
@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity saveProduct( @Valid @RequestBody Product product){
productService.saveProduct(product);
return new ResponseEntity("Product saved successfully", HttpStatus.OK);
}
My entity with the validations annotations :
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
// @ApiModelProperty(notes = "The database generated product ID")
private Integer id;
@Version
// @ApiModelProperty(notes = "The auto-generated version of the product")
@NotNull
private Integer version;
// @ApiModelProperty(notes = "The application-specific product ID" )
private String productId;
// @ApiModelProperty(notes = "The product description")
@NotBlank
@Size(max = 50)
private String description;
// @ApiModelProperty(notes = "The image URL of the product")
private String imageUrl;
// @ApiModelProperty(notes = "The price of the product", required = true)
@NotNull
private BigDecimal price;
But when I check the documentation I don't have those validation information:
Here https://github.com/springfox/springfox/issues/987 they say that we need to update our dependencies and it's what I did :
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
Did I miss something in the configuration? Any idea to help me ?
I found the solution in this post : http://vojtechruzicka.com/documenting-spring-boot-rest-api-swagger-springfox/. All is explained :
Unfortunately, JSR-303 based documentation does not work out of the box, you need an additional dependency:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.8.0</version>
</dependency>
And you need to import BeanValidatorPluginsConfiguration configuration file on top of your swagger configuration class:
@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class SpringFoxConfig {
...
}
Thank you @vojtech-ruzicka https://stackoverflow.com/users/4560142/vojtech-ruzicka
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