json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

com.networknt.schema.JsonSchemaException: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.JsonNode.canConvertToExactIntegral()Z

Open elmarm88 opened this issue 3 years ago • 2 comments

I am trying to validate a JsonNode (which I built up from a JSON string) against a JSON Schema document (Located in my resources folder). I am using NetBeans 8.2 and java 1.8 btw. (Don't ask, I know its old but its what I have to work with)

My code looks as follows

private ValidationReturn validateJsonAgainstSchema(String json, JsonSchema schema) {
    LG.info("Validating json schema");
    ValidationReturn vr = new ValidationReturn();
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonN;
    try {
      jsonN = objectMapper.readTree(json);
      Set<ValidationMessage> validationResult = schema.validate(jsonN);

I use Maven as dependancy managent and I have put in all the possible dependancies I could see might be needed in my pom file:

        <dependency>
            <groupId>com.networknt</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>1.0.71</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.13.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.13.3</version>
        </dependency>

When the code hits the Set<ValidationMessage> validationResult = schema.validate(jsonN); I get the following stacktrace:

2022-07-14 16:19:23,681  INFO sun.java.ws.rest.sunstudent.api.events.SunStudentEventService:242 - Validating json schema
 2022-07-14 16:19:23,695 ERROR sun.java.ws.rest.sunstudent.api.events.SunStudentEventService:271 - Error validating JsonSchema
 com.networknt.schema.JsonSchemaException: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.JsonNode.canConvertToExactIntegral()Z
	at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:286) ~[json-schema-validator-1.0.71.jar:?]
	at com.networknt.schema.ValidationContext.newValidator(ValidationContext.java:57) ~[json-schema-validator-1.0.71.jar:?]
	at com.networknt.schema.JsonSchema.read(JsonSchema.java:203) ~[json-schema-validator-1.0.71.jar:?]
	at com.networknt.schema.JsonSchema.getValidators(JsonSchema.java:479) ~[json-schema-validator-1.0.71.jar:?]
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:278) ~[json-schema-validator-1.0.71.jar:?]
	at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:69) ~[json-schema-validator-1.0.71.jar:?]
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:279) ~[json-schema-validator-1.0.71.jar:?]
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:262) ~[json-schema-validator-1.0.71.jar:?]
	at sun.java.ws.rest.sunstudent.api.events.SunStudentEventService.validateJsonAgainstSchema(SunStudentEventService.java:248) 

Any ideas would be greatly appeciated.

Elmar Matthee

elmarm88 avatar Jul 14 '22 14:07 elmarm88

It looks like you have other dependencies to downgrade your Jackson to a lower version. Try to get the dependency tree and check it.

stevehu avatar Jul 16 '22 17:07 stevehu

We initially had this error as well and stevehu is right. You need Jackson databind >= 2.12 iirc and we had to force the dependency version.

rustermi avatar Jul 26 '22 08:07 rustermi

I'm having the exact same issue as the OP.

Strangely (and perhaps the cause of the problem) is that everything works fine when I run the project locally through IntelliJ (Windows). The error is thrown when it runs inside a Docker container (through WSL or Ubuntu).

Maven dependencies include spring-boot 2.75 (current latest) which depends on Jackson 2.14. Every Jackson library is set to 2.14 (current latest), of course including the databind one.

Upon analyzing the dependency tree, there are no conflicts related to any Jackson dependency.

StefanGhiociFortech avatar Nov 08 '22 10:11 StefanGhiociFortech