[SpringMVC]JSON的格式
目录结构
package com.zlc.controller; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.zlc.pojo.User; import com.zlc.utils.JsonUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @Controller //相当于ResponseBody 不走视图解析器,直接返回字符串,二选一 //@RestController public class UserController { @RequestMapping("/j1") @ResponseBody //不会走视图解析器,直接返回字符串 public String json1(){ //创建一个对象 User user=new User("赵路仓",21,"男"); //对象转成字符串 return user.toString(); } @RequestMapping("/j2") @ResponseBody //不会走视图解析器,直接返回字符串 public String json2() throws JsonProcessingException { //jackson ObjectMapper mapper=new ObjectMapper(); //创建一个集合 List<User> userList =new ArrayList<User>(); //创建对象 User user1=new User("1号",21,"男"); User user2=new User("2号",21,"男"); User user3=new User("3号",21,"男"); User user4=new User("4号",21,"男"); //将对象添加到集合内 userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); String str=mapper.writeValueAsString(userList); return str; } @RequestMapping("/j3") @ResponseBody //不会走视图解析器,直接返回字符串 public String json3() throws JsonProcessingException { //jackson ObjectMapper mapper=new ObjectMapper(); //创建一个对象 User user=new User("赵路仓",21,"男"); String str=mapper.writeValueAsString(user); return str; } //纯Java方式 @RequestMapping("/j4") @ResponseBody //不会走视图解析器,直接返回字符串 public String json4() throws JsonProcessingException { //jackson ObjectMapper mapper=new ObjectMapper(); //时间 Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间戳 return mapper.writeValueAsString(sdf.format(date)); } //Mapper格式方式 @RequestMapping("/j5") @ResponseBody //不会走视图解析器,直接返回字符串 public String json5() throws JsonProcessingException { //jackson ObjectMapper mapper=new ObjectMapper(); //使用ObjectMapper来格式化输出 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(sdf); //时间 Date date=new Date(); //时间戳 return mapper.writeValueAsString(sdf.format(date)); } //工具类复用 @RequestMapping("/j6") @ResponseBody //不会走视图解析器,直接返回字符串 public String json6() throws JsonProcessingException { Date date=new Date(); return JsonUtils.getJson(date); } //fastjson,阿里开源,Java对象转换为字符串 @RequestMapping("/j7") @ResponseBody //不会走视图解析器,直接返回字符串 public String json7() throws JsonProcessingException { //创建一个集合 List<User> userList =new ArrayList<User>(); //创建对象 User user1=new User("1号",21,"男"); User user2=new User("2号",21,"男"); User user3=new User("3号",21,"男"); User user4=new User("4号",21,"男"); //将对象添加到集合内 userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); String str= JSON.toJSONString(userList); return str; } }
package com.zlc.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private String name; private int age; private String sex; }
package com.zlc.utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import java.text.SimpleDateFormat; 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 dateFormat){ ObjectMapper mapper=new ObjectMapper(); //使用ObjectMapper来格式化输出 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(sdf); try { return mapper.writeValueAsString(object); }catch (JsonProcessingException e) { e.printStackTrace(); } return null; } }