Jackson Json并没有这样的方法错误。

时间:2022-08-26 17:01:42

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.

我试图用杰克逊来序列化和反序列化一个POJO。从POJO到JSON运行得很好,但反过来就不行。

I have a POJO

我有一个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;

为了运行和测试,我运行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(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)

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.

我已经尽我所能让JSON为POJO工作,但它不会。如果我从JSON映射到映射类型,它就会工作。

Thanks for the help

谢谢你的帮助

EDIT

here is a grep for jackson in my dependencies

这是杰克逊在我的属地

± > 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

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

除了杰克逊2,似乎没有其他版本的杰克逊了。

The full method being run is a spring controller method.

正在运行的完整方法是spring controller方法。

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

1 个解决方案

#1


60  

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.

看起来问题在于,你得到的是与jackson-core和jackson-databind不兼容的版本。

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.

异常的第一行告诉您,它找不到JsonParser.getValueAsString()方法,在API文档中查找2.0.5,这个方法确实不存在。看起来它是在2.1.0中添加的。

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

因此,您将需要修复依赖关系——最可能的方法是排除2.0.5和2.1.0。

#1


60  

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.

看起来问题在于,你得到的是与jackson-core和jackson-databind不兼容的版本。

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.

异常的第一行告诉您,它找不到JsonParser.getValueAsString()方法,在API文档中查找2.0.5,这个方法确实不存在。看起来它是在2.1.0中添加的。

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

因此,您将需要修复依赖关系——最可能的方法是排除2.0.5和2.1.0。

相关文章