Search code examples
mavenjakarta-eejbossswaggerhibernate-validator

javax validation with swagger & jboss


I'm trying to use swagger to document my rest api, and I've had some success, but I'm hitting a wall when it comes to constrained resource parameters. I'm using JBoss RESTEasy with hibernate validator provider and Jackson 2.x annotated POJOs. The problem I'm hitting is when @DecimalMin/@DecimalMax annotations are encountered I get a

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

It seems that swagger is trying to use a newer version of the javax.validation.constraints.* (v1.1.0.Final) library and jboss is using an older version (v1.0.0.GA). I've tried everything I can think of to include the proper library to no avail.

Environment is JBoss EAP 6.4.0.GA

My swagger entry (pom.xml):

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

Example POJO Snippet:

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

I've tried a lot of things, but the more I learned, I believe the below are my two most relevant attempts to straighten out my dependencies:

Attempt 1: Exclude javax.validation from hibernate validator & let swagger include it:

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

Attempt 2: Include javax.validation v1.1.0.Final explicitly:

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

Maven Dependency Tree (attempt 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

So my question is: How do I use Swagger with JBoss RESTEasy & Hibernate Validator?

P.S. I'm new to swagger/jboss/javaee so please forgive any silly questions and don't hesitate to ask for more information. Not exactly sure what all I should be providing here.


Solution

  • It turns out JBoss EAP was providing its own version of the validation library (an old version).

    I was able copy in the correct validation .jar file and edit the module.xml file at:

    $JBOSS_HOME/modules/system/layers/base/javax/validation/api/main
    

    Then point JBoss to the correct validation library:

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