FasterXML / jackson-modules-java8

Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)
Apache License 2.0
401 stars 117 forks source link

[Bug] [Help resolving missing deps needed] Exception in `JavaTimeModule` #101

Closed hellboy81 closed 5 years ago

hellboy81 commented 5 years ago

Deployed artifacts:

META-INF/lib/jackson-annotations-2.9.8.jar
META-INF/lib/jackson-core-2.9.8.jar
META-INF/lib/jackson-databind-2.9.8.jar
META-INF/lib/jackson-datatype-jsr310-2.9.8.jar
META-INF/lib/jackson-datatype-threetenbp-2.6.4.jar

// com.fasterxml.jackson.datatype.jsr310.JavaTimeModule

// then serializers:
addSerializer(Duration.class, DurationSerializer.INSTANCE);
addSerializer(Instant.class, InstantSerializer.INSTANCE);
addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE);
addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE);
addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE);
addSerializer(MonthDay.class, MonthDaySerializer.INSTANCE);
addSerializer(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE);
addSerializer(OffsetTime.class, OffsetTimeSerializer.INSTANCE);
addSerializer(Period.class, new ToStringSerializer(Period.class)); // Causes exception
addSerializer(Year.class, YearSerializer.INSTANCE);
addSerializer(YearMonth.class, YearMonthSerializer.INSTANCE);

java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;)V from class com.fasterxml.jackson.datatype.jsr310.JavaTimeModule

1669833 [INFO] org.codehaus.cargo.maven2.ContainerStartMojo - [talledLocalContainer] java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;)V from class com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
1669833 [INFO] org.codehaus.cargo.maven2.ContainerStartMojo - [talledLocalContainer]    at com.fasterxml.jackson.datatype.jsr310.JavaTimeModule.<init>(JavaTimeModule.java:168)
GedMarc commented 5 years ago

not related to : https://github.com/FasterXML/jackson-modules-java8/issues/99#issuecomment-461680457 ?

GedMarc commented 5 years ago

@hellboy81 try with jackson-datatype-jdk8 instead of jsr310

hellboy81 commented 5 years ago

@Override
public void setupModule(SetupContext context) {
        context.addSerializers(new Jdk8Serializers());
         // Exception
        context.addDeserializers(new Jdk8Deserializers());
        // And to fully support Optionals, need to modify type info:
        context.addTypeModifier(new Jdk8TypeModifier());

        // Allow enabling "treat Optional.empty() like Java nulls"
        if (_cfgHandleAbsentAsNull) {
            context.addBeanSerializerModifier(new Jdk8BeanSerializerModifier());
        }
}

329241 [INFO] org.codehaus.cargo.maven2.ContainerStartMojo - [talledLocalContainer] java.lang.VerifyError: Cannot inherit from final class
329241 [INFO] org.codehaus.cargo.maven2.ContainerStartMojo - [talledLocalContainer]     at com.fasterxml.jackson.datatype.jdk8.Jdk8Module.setupModule(Jdk8Module.java:30)
329241 [INFO] org.codehaus.cargo.maven2.ContainerStartMojo - [talledLocalContainer]     at com.fasterxml.jackson.databind.ObjectMapper.registerModule(ObjectMapper.java:550)
GedMarc commented 5 years ago

This is a custom class inheriting the jdk 8/jsr310 module?

GedMarc commented 5 years ago
ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.registerModule(new com.fasterxml.jackson.datatype.jdk8.Jdk8Module());

        TimeTestClass test = new TimeTestClass();
        System.out.println(mapper.writeValueAsString(test));

This works fine - I think your container is trying to proxy the module class? Or you are extending the module class instead of copy and paste it. What cargo module are you running?

cowtowncoder commented 5 years ago

That certainly sounds like a version mismatch: that is, a dependency compiled against certain jackson-databind version being included with different (usually older) minor version of jackson-databind. Note that minor versions of different components SHOULD match, although most of the time they need to be partially ordered: that is, jackson-databind 2.7, for example, is ok with datatype modules 2.7.x, 2.8.x and 2.9.x -- but NOT the other way around.

cowtowncoder commented 5 years ago

Btw, this:

META-INF/lib/jackson-datatype-threetenbp-2.6.4.jar

looks suspicious: if it follows same versioning scheme, you should include 2.9.x version (or whatever is latest after 2.6). I think that as per:

https://mvnrepository.com/artifact/com.github.joschi.jackson/jackson-datatype-threetenbp

you should try 2.8.4. This is not necessarily the problem but might be related.

huyle1097 commented 3 years ago

I used jackson-datatype-jsr310 along with jackson-jaxrs-json-provider, version 2.10.3, and have the same issue. 😢

I tried several ways but was unable to resolve it.

But none works. One time, I tried to exclude the module com.fasterxml.jackson.core in jackson-jaxrs-json-provider the error is gone but I get another one and also unable to fix it:

Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectReader.forType(Lcom/fasterxml/jackson/databind/JavaType;)Lcom/fasterxml/jackson/databind/ObjectReader; at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:802) at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634) at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:604)

@cowtowncoder any good idea that I can try to resolve this issue?

cowtowncoder commented 3 years ago

@huyle1097 You have a version incompatibility. You need to find which version(s) of Jackson jar(s) are out-of-date. Most likely jackson-databind.

huyle1097 commented 3 years ago

Thank you @cowtowncoder , I have several wars running on a Wildfly server in my scenario. And because my new war package uses a lib that contains the new Jackson version, whilst other legacy wars used the old one, it caused the issue.