blu*_*sky 156 java ajax jackson
这豆'州':
public class State {
private boolean isSet;
@JsonProperty("isSet")
public boolean isSet() {
return isSet;
}
@JsonProperty("isSet")
public void setSet(boolean isSet) {
this.isSet = isSet;
}
}
Run Code Online (Sandbox Code Playgroud)
使用ajax'success'回调通过线路发送:
success : function(response) {
if(response.State.isSet){
alert('success called successfully)
}
Run Code Online (Sandbox Code Playgroud)
这里需要注释@JsonProperty吗?使用它有什么好处?我想我可以删除这个注释而不会产生任何副作用.
在https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations上阅读这个注释我不知道什么时候需要使用它?
Old*_*eon 203
这是一个很好的例子.我用它来重命名变量,因为JSON来自一个.Net属性以大写字母开头的环境.
public class Parameter {
@JsonProperty("Name")
public String name;
@JsonProperty("Value")
public String value;
}
Run Code Online (Sandbox Code Playgroud)
这正确地解析到JSON:
"Parameter":{
"Name":"Parameter-Name",
"Value":"Parameter-Value"
}
Run Code Online (Sandbox Code Playgroud)
gre*_*pit 35
我认为OldCurmudgeon和StaxMan都是正确的,但这里有一句简单的例子给你答案.
@JsonProperty(name)告诉Jackson ObjectMapper将JSON属性名称映射到带注释的Java字段的名称.
//example of json that is submitted
"Car":{
"Type":"Ferrari",
}
//where it gets mapped
public static class Car {
@JsonProperty("Type")
public String type;
}
Run Code Online (Sandbox Code Playgroud)
Ric*_*eek 33
以及它现在的价值......除了通常的序列化和反序列化之外,JsonProperty还用于为变量指定getter和setter方法.例如,假设您有这样的有效负载:
{
"check": true
}
Run Code Online (Sandbox Code Playgroud)
和反序列化器类:
public class Check {
@JsonProperty("check") // It is needed else Jackson will look got getCheck method and will fail
private Boolean check;
public Boolean isCheck() {
return check;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在这种情况下需要JsonProperty注释.但是如果你在课堂上也有一个方法
public class Check {
//@JsonProperty("check") Not needed anymore
private Boolean check;
public Boolean getCheck() {
return check;
}
}
Run Code Online (Sandbox Code Playgroud)
也请看一下这个文档:http: //fasterxml.github.io/jackson-annotations/javadoc/2.3.0/com/fasterxml/jackson/annotation/JsonProperty.html
Sta*_*Man 13
如果没有注释,推断的属性名称(从JSON匹配)将被"设置",而不是 - 似乎是意图 - "isSet".这是因为根据Java Beans规范,形式为"isXxx"和"setXxx"的方法用于表示存在要管理的逻辑属性"xxx".
添加 JsonProperty 还可以确保安全,以防有人决定更改属性名称之一,而没有意识到相关类将被序列化为 Json 对象。如果他们更改了属性名称,则 JsonProperty 确保它将在 Json 对象中使用,而不是属性名称。
如您所知,这完全是关于对象的序列化和脱盐。假设有一个对象:
public class Parameter {
public String _name;
public String _value;
}
Run Code Online (Sandbox Code Playgroud)
这个对象的序列化是:
{
"_name": "...",
"_value": "..."
}
Run Code Online (Sandbox Code Playgroud)
变量名直接用于序列化数据。如果您要从系统实现中删除系统 api,在某些情况下,您必须在序列化/反序列化中重命名变量。@JsonProperty 是一个元数据,用于告诉序列化程序如何序列化对象。它用于:
来自示例:
public class Parameter {
@JsonProperty(
value="Name",
required=true,
defaultValue="No name",
access= Access.READ_WRITE)
public String _name;
@JsonProperty(
value="Value",
required=true,
defaultValue="Empty",
access= Access.READ_WRITE)
public String _value;
}
Run Code Online (Sandbox Code Playgroud)
除了其他答案之外,如果您在没有无参数构造函数的类中@JsonProperty使用注释,则注释非常重要。@JsonCreator
public class ClassToSerialize {
public enum MyEnum {
FIRST,SECOND,THIRD
}
public String stringValue = "ABCD";
public MyEnum myEnum;
@JsonCreator
public ClassToSerialize(MyEnum myEnum) {
this.myEnum = myEnum;
}
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
ClassToSerialize classToSerialize = new ClassToSerialize(MyEnum.FIRST);
String jsonString = mapper.writeValueAsString(classToSerialize);
System.out.println(jsonString);
ClassToSerialize deserialized = mapper.readValue(jsonString, ClassToSerialize.class);
System.out.println("StringValue: " + deserialized.stringValue);
System.out.println("MyEnum: " + deserialized.myEnum);
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,唯一的构造函数被标记为@JsonCreator,因此 Jackson 将使用此构造函数来创建实例。但输出是这样的:
序列化:{"stringValue":"ABCD","myEnum":"FIRST"}
线程“main”中的异常 com.fasterxml.jackson.databind.exc.InvalidFormatException:无法 从字符串值“stringValue”构造 ClassToSerialize$MyEnum 的实例:值不是声明的 Enum 实例名称之一:[第一、第二、第三]
@JsonProperty但是在构造函数中添加注解后:
@JsonCreator
public ClassToSerialize(@JsonProperty("myEnum") MyEnum myEnum) {
this.myEnum = myEnum;
}
Run Code Online (Sandbox Code Playgroud)
反序列化成功:
序列化:{"myEnum":"FIRST","stringValue":"ABCD"}
字符串值:ABCD
我的枚举:第一
除了上面的所有答案之外,不要忘记文档中说的部分
标记注释可用于将非静态方法定义为逻辑属性的“setter”或“getter”(取决于其签名),或将用作(序列化、反序列化)的非静态对象字段作为逻辑属性财产。
如果您non-static的类中有一个不是传统的方法,getter or setter那么您可以getter and setter通过在其上使用注释来使其表现得像 a 。看下面的例子
public class Testing {
private Integer id;
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getIdAndUsername() {
return id + "." + username;
}
public String concatenateIdAndUsername() {
return id + "." + username;
}
}
Run Code Online (Sandbox Code Playgroud)
当上述对象被序列化时,响应将包含
getUsername() getId() getIdAndUsername*由于该方法getIdAndUsername开始于getthen 它被视为普通的 getter 因此,为什么您可以使用@JsonIgnore.
如果您注意到concatenateIdAndUsername没有返回 ,那是因为它的名称没有以开头,get并且如果您希望该方法的结果包含在响应中,那么您可以使用@JsonProperty("...")它,它会被视为getter/setter上述突出显示的文档中提到的正常.
| 归档时间: |
|
| 查看次数: |
329018 次 |
| 最近记录: |