没有像默认构造一样的创建者存在):不能在[Source:(String)中反序列化Object值(没有基于委托或属性的Creator)

时间:2018-04-09 19:13:23

标签: constructor jackson deserialization dropwizard

我有一个包含以下三个构造函数的实体

public MyObject() {
   // Default constructor to avoid ambiguity
  }


    public MyObject(String arg1, String arg2, String arg3) {

        AnotherType anothertype = new AnotherType(arg1, arg2, arg3);
        this.anotherTpe = anotherTpe;
      }

      public MyObject(AnotherType anotherType) {

        this.anotherType = anotherType;
      }

然后我写了一个测试来检查desrialization为fllows

@Test   public void serializesToJSON()抛出异常{

final MyObject myObject =
    new MyObject(new AnotherType("arg1", "ar2", "arg3"));

ObjectMapper mapper = new ObjectMapper();
assertThat(MAPPER.readValue(fixture("fixtures/myObject.json"), MyObject.class))
    .isEqualTo(myobject);

}

失败并显示错误

cannot construct instance of `AnotherType` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)

我无法控制' AnotherType'对象,如果它期望该类中的默认没有args构造函数,如果这是问题。我该怎么解决这个问题?

我尝试过更多的事情

尝试按如下方式初始化映射器

ObjectMapper mapper = new ObjectMapper().enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
        .registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

并将后续内容添加到POM

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <fork>true</fork>
                        <compilerArgs>
                            <arg>-Xlint</arg>
                            <arg>-parameters</arg>
                        </compilerArgs>

                    </configuration>
                </plugin>

0 个答案:

没有答案