use*_*mda 5 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;
}
Run Code Online (Sandbox Code Playgroud)
然后我编写一个测试来检查反序列化的流程
@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);
Run Code Online (Sandbox Code Playgroud)
}
此操作失败并出现错误
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)
Run Code Online (Sandbox Code Playgroud)
AnotherType如果“ ”对象需要该类中的默认无参数构造函数(如果这是问题),我无法控制该对象。我应该如何解决这个问题?
我尝试过的其他一些事情
尝试按如下方式初始化映射器
ObjectMapper mapper = new ObjectMapper().enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
Run Code Online (Sandbox Code Playgroud)
并将以下内容添加到 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14596 次 |
| 最近记录: |