JSONJSON(JavaScript Object Notation，JS标记对象)是一种轻量级的数据交换格式。1、jackson导入依赖 com.fasterxml.jackson.core jackson-databind 2.12.0 Utils工具类package com.sw.utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import java.text.SimpleDateFormat; /** * @Author suaxi * @Date 2020/12/17 16:08 */ public class JsonUtils { public static String getJson(Object object){ return getJson(object,"yyyy-MM-dd HH:mm:ss"); } public static String getJson(Object object,String dateFormate){ ObjectMapper mapper = new ObjectMapper(); //不使用时间戳 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); //自定义时间格式 SimpleDateFormat sdf = new SimpleDateFormat(dateFormate); mapper.setDateFormat(sdf); try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } } Controllerpackage com.sw.controller; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.sw.pojo.User; import com.sw.utils.JsonUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @Author suaxi * @Date 2020/12/17 15:38 */ //@Controller @RestController public class UserController { @RequestMapping("/j1") //@ResponseBody //不会走视图解析器，直接返回一个字符串 public String test01() throws JsonProcessingException { //jackson ObjectMapper ObjectMapper mapper = new ObjectMapper(); User user = new User("孙笑川", 33, "男"); String s = mapper.writeValueAsString(user); return s; } @RequestMapping("/j2") public String test02() throws JsonProcessingException { List<User> userList = new ArrayList<User>(); User user1 = new User("孙笑川1", 33, "男"); User user2 = new User("孙笑川2", 33, "男"); User user3 = new User("孙笑川3", 33, "男"); userList.add(user1); userList.add(user3); userList.add(user2); return JsonUtils.getJson(userList); } @RequestMapping("/j3") public String test03() throws JsonProcessingException { return JsonUtils.getJson(new Date()); } } 2、fastjson com.alibaba fastjson 1.2.75 Controllerpackage com.sw.controller; import com.alibaba.fastjson.JSON; import com.sw.pojo.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; /** * @Author suaxi * @Date 2020/12/17 15:38 */ //@Controller @RestController public class UserController { @RequestMapping("/j4") public String test04(){ List<User> userList = new ArrayList<User>(); User user1 = new User("孙笑川1", 33, "男"); User user2 = new User("孙笑川2", 33, "男"); User user3 = new User("孙笑川3", 33, "男"); userList.add(user1); userList.add(user3); userList.add(user2); String s = JSON.toJSONString(userList); return s; } }