【问题标题】:Swagger-Springfox + WebFlux + JavaX validation (JSR 303) supportSwagger-Springfox + WebFlux + JavaX 验证 (JSR 303) 支持
【发布时间】:2018-11-01 15:27:13
【问题描述】:

我正在尝试为文档实现 Swagger,以便它同时选择 JSR 303 验证注释和基于 Spring Webflux 的类。

在Spring Boot项目中,我目前正在使用:

  1. 一些类的 Spring Webflux 框架
  2. 用于 Bean 验证和约束的域类的 JSR 303 注释
  3. 与 Spring Fox 框架的 Swagger 集成以生成 OpenAPI 文档。

在 build.gradle 中,使用了以下依赖项,我可以让 Spring Webflux + SpringFox 工作,但只有 JSR 303 出现问题:

springBootVersion = '2.0.2.RELEASE'

//JavaX-validation    
'javax.validation:validation-api:2.0.0.Final',    
'javax.el:javax.el-api:3.0.0',    
'org.glassfish.web:javax.el:2.2.6'

//SpringFox-Swagger    
'io.springfox:springfox-swagger2:2.9.2',    
'io.springfox:springfox-spring-webflux:3.0.0-SNAPSHOT',    
'io.github.swagger2markup:swagger2markup-gradle-plugin:1.3.3'

这是问题:

当添加以下依赖实现“Spring Webflux + SpringFox + JSR 303”,

**'io.springfox:springfox-bean-validators:2.9.2'**

我收到以下错误,

[WARN ] 2018-11-01 10:16:50.535 [main] SpringApplication - Unable to close ApplicationContext    
java.lang.IllegalStateException: Failed to introspect Class 
             springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]  

    at 
     org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:659) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]    
         at 
     org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:556 ) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]     
        at 
     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.ge tTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:724) ~[spring- beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]    
         at 
     org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:861) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]    
        at 
     org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:810) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]    
        at 
     org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]     
     Caused by: java.lang.NoClassDefFoundError: 
     org/springframework/web/servlet/HandlerMapping
        at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_92]
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source) ~[?:1.8.0_92]
        at java.lang.Class.getDeclaredMethods(Unknown Source) ~[?:1.8.0_92]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        ... 20 more            
    Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.HandlerMapping
        at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_92]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_92]

有几个博客说,SpringBoot 版本 2x 不支持 JSR 303。我不确定,但我尝试看到它不起作用。

请指教。

另外,如果目前不支持 JSR 303,除了 JSR 303 之外,还有哪些 Validation annotations 可以使用? 我不想使用 Swagger 核心注解,只是因为它破坏了代码的可读性。

【问题讨论】:

    标签: swagger spring-webflux reactive springfox jsr


    【解决方案1】:

    在你的 pom.xml 中添加:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
            <scope>compile</compile>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-webflux</artifactId>
            <version>3.0.0</version>
        </dependency>
    

    并使用注解:

    @EnableSwagger2
    

    在你招摇的配置中。

    【讨论】:

      【解决方案2】:

      springfox 官方不支持 spring webflux(更多信息可以在这里阅读:springfox issues)。 但是你可以试试这样的 SNAPSHOT 版本:webflux-swagger

      正如我看到的大于 2.3.2 的版本,支持 bean 验证注解:pringfox-support-for-jsr-303

      【讨论】:

      • 我厌倦了使用 SNAPSHOT 但它没有定位库。 pom.xml 报错
      猜你喜欢
      • 2018-07-28
      • 1970-01-01
      • 2017-11-06
      • 2015-01-10
      • 1970-01-01
      • 2011-10-30
      • 2018-08-15
      • 1970-01-01
      • 2018-03-08
      相关资源
      最近更新 更多