Java JsonProperty 注释在编组时不起作用
Java JsonProperty annotation not working when marshalling
我有以下 POJO(在构造函数等中):
public class GlobalData implements Serializable {
//constructors getters and setters
@org.codehaus.jackson.annotate.JsonProperty("size-guide")
private List<SizeGuide> sizeGuides;
}
属性 sizeGuide 标记为 JsonProperty ,因此当我使用 ObjectMapper 编组此属性时,该属性将出现在 JSON 命名的 size-guide 而不是 sizeGuide 中。
然而,这不起作用,当我将 ObjectMapper.write 值作为字符串方法执行时,该属性没有 "change" 他的名字,它仍然显示为 sizeGuide。
有什么提示吗?
您需要使用
public class GlobalData implements Serializable {
//constructors getters and setters
@com.fasterxml.jackson.annotation.JsonProperty("size-guide")
private List<SizeGuide> sizeGuides;
}
您可能还想按照上面的方式为 getSizeGuides
注释 getter - 对我来说,变量注释有时不起作用,因为 getter 似乎在 (un) 时获得优先权编组 JSON。
一些解释和例子在这里:http://www.javabyexamples.com/how-to-change-property-name-with-jackson/
我有以下 POJO(在构造函数等中):
public class GlobalData implements Serializable {
//constructors getters and setters
@org.codehaus.jackson.annotate.JsonProperty("size-guide")
private List<SizeGuide> sizeGuides;
}
属性 sizeGuide 标记为 JsonProperty ,因此当我使用 ObjectMapper 编组此属性时,该属性将出现在 JSON 命名的 size-guide 而不是 sizeGuide 中。
然而,这不起作用,当我将 ObjectMapper.write 值作为字符串方法执行时,该属性没有 "change" 他的名字,它仍然显示为 sizeGuide。
有什么提示吗?
您需要使用
public class GlobalData implements Serializable {
//constructors getters and setters
@com.fasterxml.jackson.annotation.JsonProperty("size-guide")
private List<SizeGuide> sizeGuides;
}
您可能还想按照上面的方式为 getSizeGuides
注释 getter - 对我来说,变量注释有时不起作用,因为 getter 似乎在 (un) 时获得优先权编组 JSON。
一些解释和例子在这里:http://www.javabyexamples.com/how-to-change-property-name-with-jackson/