long類型精度丟失問(wèn)題
package com.csw.shuanfa.GlobalConfig.format;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
/**
* 【此方法僅僅用于返回格式化,如果是入?yún)⒌脑掃€要手動(dòng)加注解特別是LocalDateTime】
* 全局日期格式化 如果某個(gè)字段不使用該格式
* 依舊可以使用 @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd") 修改某個(gè)字段的格式化信息,且@JsonFormat優(yōu)先級(jí)高于@JsonComponent配置的格式類型
*
* LocalDateTime注意-單個(gè)時(shí)間接收
* public Result<LocalDateTime> aa(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
* LocalDateTime注意-嵌套對(duì)象接收(兩個(gè)注解是不一樣的)
* @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
* private LocalDateTime date;
*
* 如果需要打印日志,拿到的參數(shù)里面含有T有兩種解決方法(obj可以手單個(gè)LocalDateTime,也可以是單個(gè)實(shí)體包含LocalDateTime的對(duì)象)
* 方法一
* String string = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString();
* 方法二
* SerializeConfig serializeConfig = new SerializeConfig();
* serializeConfig.put(LocalDateTime.class, new LocalDateTimeSerializer());
* String json = JSON.toJSONString(date, serializeConfig);
*
*
* private static class LocalDateTimeSerializer implements ObjectSerializer {
* private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
*
* @Override
* public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
* LocalDateTime dateTime = (LocalDateTime) object;
* String formattedDateTime = dateTime.format(formatter);
* serializer.write(formattedDateTime);
* }
* }
*
*/
@JsonComponent
@Component
public class DateFormatConfig {
private String pattern = "yyyy-MM-dd HH:mm:ss";
/**
* 類型全局時(shí)間格式化
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() {
return builder -> {
// long -> String
builder.serializerByType(Long.TYPE, ToStringSerializer.instance);
// Long -> String
builder.serializerByType(Long.class, ToStringSerializer.instance);
// Long -> String
builder.serializerByType(BigDecimal.class, ToStringSerializer.instance);
//序列化
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)));
};
}
}
//將Long.TYPE(也就是基本類型long)序列化為字符串。這意味著當(dāng)你使用這個(gè)ObjectMapper實(shí)例將包含long類型字段的對(duì)象序列化為JSON時(shí),這些字段的值會(huì)被轉(zhuǎn)換成字符串形式。
//將Long.class(也就是包裝類型Long)序列化為字符串。這與上一步類似,但是針對(duì)的是Long對(duì)象,而不是基本類型long。
日期全局返回格式化,【需要特殊格式用下面指定】或者讓前端截取
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8:00")
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
serialization:
fail-on-empty-beans: false
deserialization:
READ_UNKNOWN_ENUM_VALUES_AS_NULL: true
//date-format: 這個(gè)屬性設(shè)置了日期和時(shí)間的序列化和反序列化的格式。在這里,它被設(shè)置為 yyyy-MM-dd HH:mm:ss,意味著日期和時(shí)間將被格式化為“年-月-日 時(shí):分:秒”的形式。例如,日期時(shí)間 2024-10-14 12:00:00 將按照這種格式進(jìn)行序列化和反序列化。
//time-zone: 這個(gè)屬性設(shè)置了Jackson處理日期和時(shí)間時(shí)使用的時(shí)區(qū)。在這里,它被設(shè)置為 GMT+8,即中國(guó)標(biāo)準(zhǔn)時(shí)間。這意味著所有日期時(shí)間的序列化和反序列化都將使用這個(gè)時(shí)區(qū)。
//serialization.fail-on-empty-beans: 這個(gè)屬性控制當(dāng)序列化一個(gè)空的Java Bean(沒(méi)有屬性的類實(shí)例)時(shí)的行為。在這里,它被設(shè)置為 false,意味著即使一個(gè)Bean沒(méi)有任何屬性,Jackson也不會(huì)拋出異常,而是會(huì)序列化一個(gè)空的對(duì)象。
//deserialization.READ_UNKNOWN_ENUM_VALUES_AS_NULL: 這個(gè)屬性影響枚舉類型的反序列化行為。在這里,它被設(shè)置為 true,意味著如果反序列化過(guò)程中遇到未知的枚舉值,Jackson將把它解析為 null 而不是拋出異常。
富文本圖片返回去除
String patternString = "<img[^>]*>";
resVo.forEach(a -> {
a.setContent(a.getContent().replaceAll(patternString, ""));
});
前端/java/mapper去除換行符、回車符、制表符
js-> str.replace(/[\n\r\t\s]/g, '');
java->replaceAll("\\s+", "")
mapper->REPLACE(REPLACE(REPLACE(REPLACE(column_name, '\n', ''), '\r', ''), '\t', ''), ' ', '')