我有一个employeeDetails
从JSON字符串从数据存储中读取的类。
public class Employee {
private String name;
@JsonProperty("employeeDetails")
@JsonSerialize(???)
private String employeeDetailsBlob;
// getters and setters
}
现在,我想在响应中返回employeeDetails(动态类型)作为一个对象。我已使用更改了名称,@JsonProperty
而我拥有的所有示例均已使用@JsonSerialize(using = Employee.class)
。
所以我的回应应该像这样
{name: "foo", employeeDetails: { age: 21 }}
我得到的是
{name: "foo", employeeDetails: "{ age: 21 }"}
我可以添加@JsonSerialize
到类中,但是然后我必须自己处理所有字段,并在覆盖的方法中执行类似的操作。
jgen.writeFieldName("employeeDetails");
SerializedString serializedString = new SerializedString(empl.getEmployeeDetailsBlob());
jgen.writeRawValue(serializedString);
有没有一种方法可以使用注释,也只能在我要从json字符串更改为json对象的字段上使用。
添加@JsonRawValue
就可以了。
public class Employee {
private String name;
@JsonProperty("employeeDetails")
@JsonRawValue
private String employeeDetailsBlob;
// getters and setters
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句