jacksonJson并没有这样的方法错误

我正在尝试使用jackson来序列化和反序列化一个POJO。 从POJO到JSON完美地工作,但是走向另一个方向却不行。

我有一个POJO

public class Event { private String kind; public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } } 

并运行并testing我运行的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); 

抛出exception

 java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonParser.getValueAsString()Ljava/lang/String; at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:24) at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2796) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942) at calendar.controller.RootController.details(RootController.java:59) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 

我尽我所能地让JSON去POJO工作,但事实并非如此。 如果我从JSON映射到Maptypes,它会工作。

谢谢您的帮助

编辑

这里是我的依赖关系jacksongrep

 ± > mvn dependency:tree | grep jackson -I- [INFO] +- com.google.http-client:google-http-client-jackson2:jar:1.13.1-beta:compile [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 

看起来除了jackson2以外没有其他版本的jackson。

正在运行的完整方法是一个弹簧控制器方法。

 @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; } 

它看起来像问题是你得到不兼容版本的jackson-corejackson-databind – jackson核心2.0.5被拉入,但我相信至less2.1.0是必需的。

exception的第一行告诉你,它不能find方法JsonParser.getValueAsString() ,看2.0.5的API文档,该方法确实不存在。 它看起来像是在2.1.0中添加的。

所以,你需要修复依赖 – 很可能是通过排除2.0.5和2.1.0。