swagger springfox - bean 验证 JSR 303 无法识别

swagger springfox - bean validation JSR 303 not recognize

我按照本教程 https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/ 生成了 swagger 文档。 它正在工作,但是当我尝试在我的 bean 中添加一些验证时,我没有在文档中找到信息:

@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);
}

我的带有验证注释的实体:

@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;

但是当我查看文档时,我没有那些验证信息:

这里 https://github.com/springfox/springfox/issues/987 他们说我们需要更新我们的依赖项,我就是这么做的 :

<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>

我是否遗漏了配置中的某些内容?有什么想法可以帮助我吗?

我在 post 中找到了解决方案:http://vojtechruzicka.com/documenting-spring-boot-rest-api-swagger-springfox/。 一切都解释清楚了:

遗憾的是,基于 JSR-303 的文档无法开箱即用,您需要额外的依赖项:

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

并且您需要在 swagger 配置之上导入 BeanValidatorPluginsConfiguration 配置文件 class:

@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class SpringFoxConfig {
  ...
}

谢谢@vojtech-ruzicka https://whosebug.com/users/4560142/vojtech-ruzicka