JSON Chinese garbled characters
If there are garbled characters, we need to set its encoding format to utf-8, and the type it returns, through the produces property of @RequesMapping, the code is as follows
produces: Specify the response body return type and encoding
JSON handles Date format
@RequestMapping(value = "/date1" )
@ResponseBody
public String date1() throws JsonProcessingException {
//声明时间日期 注意是util下的
Date date = new Date();
//打印默认时间日期格式 Tue Feb 08 21:36:27 CST 2022
System.out.println(date);
//发现问题 时间默认返回的json字符串变成了时间戳类型的格式 Timestamp 1644327387386
return new ObjectMapper().writeValueAsString(date);
}
Found the problem: the json string returned by time by default has become the format of timestamp type Timestamp 1644327387386
Dealing with issues: How to make it not return a timestamp? How to time format issues
//Turn off the timestamp function of ObjectMapper Turn off the timestamp function through the configure method of objectMapper
**ObjectMapper objectMapper=new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
**SimpleDateFormat simpleDateFormat = new SimpleDateFormat( “yyyy-MM-dd HH:mm:ss” );
**objectMapper.setDateFormat(simpleDateFormat);
@RequestMapping(value = "/date2" )
@ResponseBody
public String date2() throws JsonProcessingException {
//1 如何让它不返回时间戳? 通过objectMapper的configure方法关闭时间戳功能
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//2 时间格式化问题 自定义日期格式对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
//3 让objectMapper指定日期格式为simpleDateFormat
objectMapper.setDateFormat(simpleDateFormat);
//声明Date
Date date = new Date();
//格式化日期并输出
System.out.println(simpleDateFormat.format(date));//2022-02-08 22:01:12
return objectMapper.writeValueAsString(date);//"2022-02-08 22:01:12"
}
Package JSON tool date conversion tool class
For the above code redundancy, we extract the public code and encapsulate it into a tool class, which can be called directly next time
Utility classes and public methods
package com.wyh.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* @program: SpringBoot-Ajax-JSON
* @description: JSON日期格式化封装类
* @author: 魏一鹤
* @createDate: 2022-02-08 22:07
**/
public class JSONDateUtil {
//重载 把时间戳也进行封装
//如果没有自定义时间格式的话就使用我们默认的yyyy-MM-dd HH:mm:ss"
//如果调用方法在参数列表指明了自定义时间格式的话就使用参数列表的时间格式
public static String JSONFormatDateUtil(Object object){
return JSONFormatDateUtil(object, "yyyy-MM-dd HH:mm:ss" );
}
// JSON转换日期的公共方法,直接传递Date类型的参数使用ObjectMapper进行处理 需要抛出异常 得到格式化好之后的json日期类型
// 把方法用static进行修饰是为了不用创建工具类对象就能直接通过“.”进行公共方法的调用
public static String JSONFormatDateUtil(Object object,String dateFormat){
//1 创建ObjectMapper工具类
ObjectMapper objectMapper=new ObjectMapper();
//2 通过ObjectMapper类的configure方法关闭时间戳功能
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//3 创建时间格式化SimpleDateFormat类 自定义日期格式对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
//4 把自定义格式化好之后的日期类型给objectMapper的setDateFormat去用,用来转换时间格式
objectMapper.setDateFormat(simpleDateFormat);
//5 json转换时间格式得到json字符串日期类型
//封装的时候尽量不要抛异常而是捕获异常 不然每次调用这个方法都要在其他地方进行抛异常
String JSONDate = null;
try {
JSONDate = objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//6 返回最终处理好的json字符串日期类型
return JSONDate;
}
}
Using utility classes and public methods
Pay attention to the parameter list and returned format of JSONDateUtil. JSONFormatDateUtil ()
//使用工具类和公共方法
@RequestMapping(value = "/date3" )
@ResponseBody
public String date3() {
//声明Date
Date date = new Date();
//使用工具类的公共方法把date进行传参转换格式
String JSONDate = JSONDateUtil.JSONFormatDateUtil(date);
//返回json日期类型
return JSONDate; //"2022-02-08 22:31:11"
}
}
//使用工具类
@RequestMapping(value = "/date3" )
@ResponseBody
public String date3() {
//声明Date
Date date = new Date();
//使用工具类的公共方法把date进行传参转换格式
String JSONDate = JSONDateUtil.JSONFormatDateUtil(date, "yyyy" );
//返回json日期类型
return JSONDate; //"2022"
}
//使用工具类
@RequestMapping(value = "/date3" )
@ResponseBody
public String date3() {
//声明Date
Date date = new Date();
//使用工具类的公共方法把date进行传参转换格式
String JSONDate = JSONDateUtil.JSONFormatDateUtil(date, "yyyy-MM-dd" );
//返回json日期类型
return JSONDate; //"2022-02-08"
}