austinbv austinbv - 1 year ago 67
Java Question

Jackson Json and no such method errors

I am trying to use jackson to serialize and deserialize a POJO. Going from POJO to JSON works perfectly but going the other direction does not.

I have a POJO

public class Event {
private String kind;

public String getKind() {
return kind;

public void setKind(String kind) {
this.kind = kind;

and to run and test I run package calendar.model;

Event event = new Event();
event.setKind("This is a kind");
String json = objectMapper.writeValueAsString(event);
// RETURNS: "{\"kind\":\"This is a kind\"}"

objectMapper.readValue(json, Event.class);

Throws Exception

java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonParser.getValueAsString()Ljava/lang/String;
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(
at com.fasterxml.jackson.databind.ObjectMapper.readValue(
at calendar.controller.RootController.details(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(

I have played with about all I can to get the JSON to POJO to work but it won't. It does work if I map from JSON to a Map type.

Thanks for the help


here is a grep for jackson in my dependencies

± > mvn dependency:tree | grep jackson -I-
[INFO] +-
[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.0.5:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.1.1:compile
[INFO] | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.1.1:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.1.1:compile
[INFO] | | | +- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.1.0:compile

It looks like there is no other version of jackson except for jackson2.

The full method being run is a spring controller method.

@RequestMapping(value = "/")
public Event root() throws IOException {
Event event = new Event();
event.setKind("This is a kind");
String json = objectMapper.writeValueAsString(event);
// RETURNS: "{\"kind\":\"This is a kind\"}"

Event mapped = objectMapper.readValue(json, Event.class);
return mapped;

Answer Source

It looks like the problem is that you are getting incompatible versions of jackson-core and jackson-databind - jackson-core 2.0.5 is being pulled in, but I believe at least 2.1.0 is required.

The first line of the exception tells you that it can't find the method JsonParser.getValueAsString(), looking at the API docs for 2.0.5, that method indeed does not exist. It looks like it was added in 2.1.0.

So, you'll need to fix the dependencies - most likely by excluding 2.0.5 and including 2.1.0.