javax验证与swagger& JBoss的

时间:2016-05-03 03:54:21

标签: maven java-ee jboss swagger hibernate-validator

我正在尝试使用招摇来记录我的休息api,并且我已经取得了一些成功,但是当涉及到受约束的资源参数时,我正在碰壁。我正在使用JBoss RESTEasy与hibernate验证器提供程序和Jackson 2.x注释POJO。我遇到的问题是遇到@ DecimalMin / @ DecimalMax注释我得到了

java.lang.NoSuchMethodError: javax.validation.constraints.DecimalMin.inclusive()Z 

似乎swagger正在尝试使用更新版本的javax.validation.constraints。*(v1.1.0.Final)库,而jboss正在使用旧版本(v1.0.0.GA)。我已经尝试了所有我能想到的东西,包括适当的库无济于事。

环境是JBoss EAP 6.4.0.GA

我的招摇条目(pom.xml):

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jaxrs</artifactId>
    <version>1.5.8</version>
</dependency>

示例POJO代码段:

...
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
...
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "altM",
    "altDatum"
 })
public class Altitude {
@JsonProperty("altM")
@DecimalMin("-2000")
@DecimalMax("10000")
@NotNull
private Double altM;
...

我已经尝试了很多东西,但是我学到的越多,我相信以下是我最理性的两个尝试来理顺我的依赖:

尝试1:从hibernate验证器&amp;中排除javax.validation。让招摇包括它:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-hibernatevalidator-provider</artifactId>
    <version>3.0.16.Final</version>
    <exclusions>
        <exclusion>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

尝试2:明确包含javax.validation v1.1.0.Final:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>

Maven依赖树(尝试1):

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @     MissionPlanningService ---
[INFO] mil.army.cerdec.cpi.mcas:MissionPlanningService:war:0.0.2-SNAPSHOT
[INFO] +- org.jboss.resteasy:resteasy-jaxrs:jar:3.0.16.Final:compile
[INFO] |  +- org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.0_spec:jar:1.0.0.Final:compile
[INFO] |  +- org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  +- javax.activation:activation:jar:1.1.1:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.3.6:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.3.3:compile
[INFO] |  |  +- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.6:compile
[INFO] |  +- commons-io:commons-io:jar:2.1:compile
[INFO] |  +- net.jcip:jcip-annotations:jar:1.0:compile
[INFO] |  \- org.jboss.logging:jboss-logging:jar:3.1.4.GA:compile
[INFO] +- org.jboss.resteasy:resteasy-hibernatevalidator-provider:jar:3.0.16.Final:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:4.3.1.Final:compile
[INFO] |  \- org.jboss.weld.se:weld-se:jar:2.1.0.Final:compile
[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] +- org.jboss.resteasy:resteasy-jackson2-provider:jar:3.0.16.Final:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.6.3:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.3:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.3:compile
[INFO] |  \- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.6.3:compile
[INFO] |     +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.6.3:compile
[INFO] |     \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.6.3:compile
[INFO] \- io.swagger:swagger-jaxrs:jar:1.5.8:compile
[INFO]    +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.4.5:compile
[INFO]    |  \- org.yaml:snakeyaml:jar:1.12:compile
[INFO]    +- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.4.5:compile
[INFO]    |  \- org.codehaus.woodstox:stax2-api:jar:3.1.4:compile
[INFO]    +- io.swagger:swagger-core:jar:1.5.8:compile
[INFO]    |  +- org.apache.commons:commons-lang3:jar:3.2.1:compile
[INFO]    |  +- org.slf4j:slf4j-api:jar:1.6.3:compile
[INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-joda:jar:2.4.5:compile
[INFO]    |  |  \- joda-time:joda-time:jar:2.2:compile
[INFO]    |  +- io.swagger:swagger-models:jar:1.5.8:compile
[INFO]    |  |  \- io.swagger:swagger-annotations:jar:1.5.8:compile
[INFO]    |  \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO]    +- javax.ws.rs:jsr311-api:jar:1.1.1:compile
[INFO]    +- org.reflections:reflections:jar:0.9.10:compile
[INFO]    |  +- org.javassist:javassist:jar:3.19.0-GA:compile
[INFO]    |  \- com.google.code.findbugs:annotations:jar:2.0.1:compile
[INFO]    \- com.google.guava:guava:jar:18.0:compile

所以我的问题是:如何将Swagger与JBoss RESTEasy&amp; Hibernate Validator?

P.S。我是swagger / jboss / javaee的新手,所以请原谅任何愚蠢的问题,不要犹豫,要求提供更多信息。不完全确定我应该在这里提供什么。

1 个答案:

答案 0 :(得分:0)

事实证明,JBoss EAP提供了自己的验证库版本(旧版本)。

我能够复制正确的验证.jar文件并编辑module.xml文件:

...

<module xmlns="urn:jboss:module:1.1" name="javax.validation.api">
    <resources>
        <!-- OLD ONE: <resource-root path="validation-api-1.0.0.GA-redhat-3.jar"/> -->
        <!-- NEW ONE BELOW: -->
        <resource-root path="validation-api-1.1.0.Final.jar"/>
        <!-- Insert resources here -->
    </resources>
    <dependencies>
        <module name="org.jboss.logging"/>
    </dependencies>
</module>

然后将JBoss指向正确的验证库:

{{1}}