[英]unable to serialize JSON to Set correctly with Spring MVC/jackson
public class Parent {
private List<Child> childs;
}
public class Parent {
private Set<Child> childs;
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public void saveFooter(@RequestBody Parent parent, Model model)
below is the sample JSON object. 以下是示例JSON对象。
{child: [{ name: 'a'}, { name: 'b'}]}
When I use Set for childs in Parent.class, parent.getChilds.size()
, the size of childs is 1 only(which is wrong). 当我在Parent.class,
parent.getChilds.size()
为孩子使用Set时 ,孩子的大小仅为1(这是错误的)。
But when I use List for childs in Parent.class, parent.getChilds.size()
, the size of childs is 2 (which is correct) 但是当我在Parent.class,
parent.getChilds.size()
为子级使用List时,子级的大小是2(正确)
Edit: 编辑:
public class Child {
private String name;
}
jackson maven dependency 杰克逊·马文(Jackson Maven)依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.4</version>
</dependency>
Edit: 编辑:
public class Child {
private Long id;
private String name;
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Child other = (Child) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
found out that the problem is due to I implemented the equals method of Child.class which make the two object equal. 发现问题是由于我实现了Child.class的equals方法而使两个对象相等。
Parent.java Parent.java
package com.test;
import java.util.Set;
public class Parent {
private Set<Child> childs;
public Set<Child> getChilds() {
return childs;
}
public void setChilds(Set<Child> childs) {
this.childs = childs;
}
}
Child.Java 子Java
package com.test;
public class Child {
private String name;
public Child(String name) {
this.name = name;
}
public Child() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Test.java Test.java
package com.test;
import java.io.IOException;
import java.util.HashSet;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import flexjson.JSONSerializer;
public class MvelCompilation {
public static void main(String[] args) {
Parent parent = new Parent();
Child c1 = new Child("Ankur1");
Child c2 = new Child("Ankur2");
HashSet<Child> childSet = new HashSet<Child>();
childSet.add(c1);
childSet.add(c2);
parent.setChilds(childSet);
JSONSerializer serializer = new JSONSerializer();
System.out.println("Json String is = " + serializer.exclude("*.class").deepSerialize(parent));
String s = "{\"childs\":[{\"name\":\"Ankur1\"},{\"name\":\"Ankur2\"}]}";
try {
Parent parent1 = new ObjectMapper().readValue(s, Parent.class);
System.out.println("size = " + parent1.getChilds().size());
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output 输出量
Json String is = {"childs":[{"name":"Ankur1"},{"name":"Ankur2"}]} Json字符串是= {“ childs”:[{“ name”:“ Ankur1”},{“ name”:“ Ankur2”}]}
size = 2 大小= 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.