@JsonSerialize(using= JsonDateSerializer.class) private Date taskEndTime; @ApiModelProperty(value = "檢查日期") @JsonFormat(pattern = "yyyy-MM-dd") private LocalDate checkDate;
//在反序列化時忽略在 json 中存在但 Java 物件不存在的屬性 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //在序列化時日期格式預設為 yyyy-MM-dd'T'HH:mm:ss.SSSZ ,比如如果一個類中有private Date date;這種日期屬性,序列化後為:{"date" : 1413800730456},若不為true,則為{"date" : "2014-10-20T10:26:06.604+0000"} mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); //在序列化時忽略值為 null 的屬性 mapper.setSerializationInclusion(Include.NON_NULL); //忽略值為預設值的屬性 mapper.setDefaultPropertyInclusion(Include.NON_DEFAULT); // 美化輸出 mapper.enable(SerializationFeature.INDENT_OUTPUT); // 允許序列化空的POJO類 // (否則會丟擲異常) mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // 把java.util.Date, Calendar輸出為數位(時間戳) mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 在遇到未知屬性的時候不丟擲異常 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // 強制JSON 空字串("")轉換為null物件值: mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); // 在JSON中允許C/C++ 樣式的註釋(非標準,預設禁用) mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); // 允許沒有引號的欄位名(非標準) mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); // 允許單引號(非標準) mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 強制跳脫非ASCII字元 mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true); // 將內容包裹為一個JSON屬性,屬性名由@JsonRootName註解指定 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); //序列化列舉是以toString()來輸出,預設false,即預設以name()來輸出 mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING,true); //序列化Map時對key進行排序操作,預設false mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS,true); //序列化char[]時以json陣列輸出,預設false mapper.configure(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS,true); //序列化BigDecimal時之間輸出原始數位還是科學計數,預設false,即是否以toPlainString()科學計數方式來輸出 mapper.configure(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS,true);
/** * java 8 LocalDateTime轉換器 * * @author wangling */ public class LocalDateTimeFormatter implements Formatter<LocalDateTime> { private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Override public LocalDateTime parse(String text, Locale locale) throws ParseException { return LocalDateTime.parse(text, formatter); } @Override public String print(LocalDateTime object, Locale locale) { return formatter.format(object); } }
/** * java 8 localDate轉換器 * * @author wangling */ public class LocalDateFormatter implements Formatter<LocalDate> { private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); @Override public LocalDate parse(String text, Locale locale) throws ParseException { return LocalDate.parse(text, formatter); } @Override public String print(LocalDate object, Locale locale) { return formatter.format(object); } }
/** * 專案全域性設定類 * * @author wangling * @date 2022/06/10 */ @Configuration @RequiredArgsConstructor public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addFormatterForFieldType(LocalDate.class, new LocalDateFormatter()); registry.addFormatterForFieldType(LocalDateTime.class, new LocalDateTimeFormatter()); } @Bean @Primary public ObjectMapper ObjectMapper() { String dateTimeFormat = "yyyy-MM-dd HH:mm:ss"; String dateFormat = "yyyy-MM-dd"; String timeFormat = "HH:mm:ss"; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JavaTimeModule javaTimeModule = new JavaTimeModule(); // 序列化 javaTimeModule.addSerializer( LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat))); javaTimeModule.addSerializer( LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat))); javaTimeModule.addSerializer( LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat))); javaTimeModule.addSerializer( Date.class, new DateSerializer(false, new SimpleDateFormat(dateTimeFormat))); // 反序列化 javaTimeModule.addDeserializer( LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat))); javaTimeModule.addDeserializer( LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat))); javaTimeModule.addDeserializer( LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat))); javaTimeModule.addDeserializer(Date.class, new DateDeserializers.DateDeserializer() { @SneakyThrows @Override public Date deserialize(JsonParser jsonParser, DeserializationContext dc) { String text = jsonParser.getText().trim(); SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormat); return sdf.parse(text); } }); javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance); javaTimeModule.addSerializer(BigInteger.class, ToStringSerializer.instance); objectMapper.registerModule(javaTimeModule); return objectMapper; } }
/** * 專案全域性設定類 * * @author wangling * @date 2022/06/10 */ @Configuration public class MvcInterceptorConfig extends WebMvcConfigurationSupport { @Override protected void addFormatters(FormatterRegistry registry) { // 用於get 全域性格式化日期轉換 registry.addFormatterForFieldType(LocalDate.class, new LocalDateFormatter()); registry.addFormatterForFieldType(LocalDateTime.class, new LocalDateTimeFormatter()); } @Override protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) { // 代替框架預設的JackSon設定 用於post 全域性格式化日期轉換,long轉字串 MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); jackson2HttpMessageConverter.setObjectMapper(ObjectMapper()); // 基於順序,先執行自定義的 converters.add(0, jackson2HttpMessageConverter); } private ObjectMapper ObjectMapper() { String dateTimeFormat = "yyyy-MM-dd HH:mm:ss"; String dateFormat = "yyyy-MM-dd"; String timeFormat = "HH:mm:ss"; ObjectMapper objectMapper = new ObjectMapper(); //忽略空Bean轉json的錯誤 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); //忽略 在json字串中存在,但是在物件中不存在對應屬性的情況,防止錯誤。 // 例如json資料中多出欄位,而物件中沒有此欄位。如果設定true,丟擲異常,因為欄位不對應;false則忽略多出的欄位,預設值為null,將其他欄位反序列化成功 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JavaTimeModule javaTimeModule = new JavaTimeModule(); // 序列化 javaTimeModule.addSerializer( LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat))); javaTimeModule.addSerializer( LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat))); javaTimeModule.addSerializer( LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat))); javaTimeModule.addSerializer( Date.class, new DateSerializer(false, new SimpleDateFormat(dateTimeFormat))); // 反序列化 javaTimeModule.addDeserializer( LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat))); javaTimeModule.addDeserializer( LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat))); javaTimeModule.addDeserializer( LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat))); javaTimeModule.addDeserializer(Date.class, new DateDeserializers.DateDeserializer() { @SneakyThrows @Override public Date deserialize(JsonParser jsonParser, DeserializationContext dc) { String text = jsonParser.getText().trim(); SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormat); return sdf.parse(text); } }); javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance); javaTimeModule.addSerializer(BigInteger.class, ToStringSerializer.instance); objectMapper.registerModule(javaTimeModule); return objectMapper; } }
WebMvcConfigurer
介面提供了很多方法讓開發者來客製化SpringMVC的設定。微信讚賞
支付寶讚賞