I have classes structured in the following way:
@JsonRootName("class1")
public class class1 {
private interface1 foo;
public interface1 getFoo() {
return foo;
}
public void setFoo(interface1 foo) {
this.foo = foo;
}
}
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({@JsonSubTypes.Type(value = class2.class, name = "class2")})
public interface interface1 {
String getType();
}
@JsonTypeName("class2")
public class class2 implements interface1 {
private String buzz = "bar";
@Override
public String getType() {
return "class2";
}
public String getBuzz() {
return buzz;
}
public void setBuzz(String buzz) {
this.buzz = buzz;
}
}
If I parsed the following JSON into class1 using the ObjectMapper
it would be fine:
{
"class1": {
"foo": {
"class2": {
"buzz": "not bar"
}
}
}
}
In this example the buzz variable in the class2 object would be set to "not bar" and the foo variable in the class1 would be set to the class2 object.
If I didn't want to modify the buzz
variable in class2
and just wanted the foo variable in class1
to be set to a plain class2
object, how would I do that? The following doesn't work since "class2"
is interpreted as a string:
{
"class1": {
"foo": "class2"
}
}
Same as the first example, but without the buzz
field.
{
"class1": {
"foo": {
"class2": {
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.