ResourceTestRule in conjunction with Java8 Bundle throws: "Can not deserialize instance of java.time.LocalDate out of START_ARRAY token"

1,839 views
Skip to first unread message

Yun Zhi Lin

unread,
Mar 13, 2015, 9:16:04 AM3/13/15
to dropwiz...@googlegroups.com
I came across a very strange error when running tests using ResourceTestRule with Java8 Bundle. I've raised an error here: https://github.com/dropwizard/dropwizard-java8/issues/8 but not certain whether the issue is with DW or Jackson.

I set up my test with:
private static final ObjectMapper MAPPER = Jackson.newObjectMapper()
        .registerModules(new JavaOptionalModule())
        .registerModules(new JSR310Module());

@ClassRule
public static ResourceTestRule resources = ResourceTestRule.builder()
        .setMapper(MAPPER)
        .addResource(new DummyResource())
        .addProvider(OptionalMessageBodyWriter.class)
        .addProvider(OptionalParamFeature.class)
        .build();


And I get the error:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.time.LocalDate out of START_ARRAY token
at
[Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@6d136eeb; line: 1, column: 2] (through reference chain: trunk.stream.model.Dummy["localDate"])
at com
.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com
.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)



It's ass if the Object Mapper isn't picking up all the JSR310 Module that I've registered when setting up the test. 

I've created a standalone unit test in this gist: https://gist.github.com/yunspace/70947f5b889816425c85 

Any guidance would be much appreciated.

Odd Christer Brovig

unread,
Mar 13, 2015, 9:47:14 AM3/13/15
to dropwiz...@googlegroups.com
Hi. I'm seeing somewhat similar issues when upgrading from 0.7.1 to 0.8. Are you running 0.8 too? In our code it fails to serialize/deserialize field with our custom jackson mappers (+jsr310).

Yun Zhi Lin

unread,
Mar 14, 2015, 8:44:11 AM3/14/15
to dropwiz...@googlegroups.com
Yes we are also using 0.8.0. It seems that the ResourceTestRule is ignoring the registered modules.

Matt Nelson

unread,
Mar 16, 2015, 12:25:31 PM3/16/15
to dropwiz...@googlegroups.com
Try using the Jdk8Module instead.

Frode Nerbråten

unread,
Mar 18, 2015, 3:54:10 PM3/18/15
to dropwiz...@googlegroups.com
I had the same problem. It seems that the ObjectMapper instance is only registered on the server side of the JerseyTest and not on the client.
This makes sense from your example since you are returning a JSR310 date from the resource, but you get a deserialize exception on the client. The server is able to serialize it correctly.


The workaround involves registering a JsonProvider on the test client with the same mapper instance:
@BeforeClass
public static void initClient() {
   
JacksonJsonProvider provider = new JacksonJsonProvider();
    provider
.setMapper(MAPPER);
    resources
.client().register(provider);
}


Regards,
Frode

Yun Zhi Lin

unread,
Mar 18, 2015, 7:40:15 PM3/18/15
to dropwiz...@googlegroups.com
Thanks Frode! This works perfectly. This work around should either be documented or ResourceTestRule should be updated accordingly. I noticed you are planning to do a PR, I'll wait for that before closing this issue.

Frode Nerbråten

unread,
Mar 19, 2015, 2:51:03 AM3/19/15
to dropwiz...@googlegroups.com
The PR got accepted, so from 0.8.1 the workaround won't be necessary any more.

Reply all
Reply to author
Forward
0 new messages