@Test
public void test2() throws JsonProcessingException {
Person p = new Person();
p.setName("Rose");
p.setAge(22);
p.setGender("女");
p.setBirthday(new Date());
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(p);
System.out.println(json);
}
//json传送一个list
@Test
public void test3() throws JsonProcessingException {
Person p = new Person();
p.setName("Haker");
p.setAge(33);
p.setGender("男");
p.setBirthday(new Date());
Person p1 = new Person();
p1.setName("Shelly");
p1.setAge(33);
p1.setGender("男");
p1.setBirthday(new Date());
Person p2 = new Person();
p2.setName("Bale");
p2.setAge(33);
p2.setGender("男");
p2.setBirthday(new Date());
List<Person> list = new ArrayList<>();
list.add(p);
list.add(p1);
list.add(p2);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(list);
ObjectMapper mapper = new ObjectMapper();
Person person = mapper.readValue(js, Person.class);
System.out.println(person);
}
}
问题解决:
@Controller
public class FileController {
@ResponseBody
@RequestMapping("/test")
public String file() throws JsonProcessingException {
List<MyFile> list = new ArrayList<>(10);
list.add(new MyFile("01", new Date()));
list.add(new MyFile("02", new Date()));
list.add(new MyFile("03", new Date()));
list.add(new MyFile("04", new Date()));
list.add(new MyFile("05", new Date()));
list.add(new MyFile("06", new Date()));
list.add(new MyFile("07", new Date()));
list.add(new MyFile("08", new Date()));
list.add(new MyFile("09", new Date()));
list.add(new MyFile("10", new Date()));
list.add(new MyFile("11", new Date()));
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(list);