刚来这家公司的时候, 发现很多同事还在用这种方式写代码
当时以为是偶然, 刚才在群里发现还有好多人在交流应当加哪些注解, 声明时区问题.
当写一个东西感到麻烦的时候, 那么大概率是有低成本的更优解的
分以下两种情况来处理
请求体
使用Jackson, 仅保留时间相关配置
package kim.nzxy.ly.common.config;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* JSON相关
*
* @author ly-chn
*/
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return builder -> {
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dtf))
.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dtf))
// 以下代码"可以, 但没必要"
.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME))
.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME))
.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE))
.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE))
};
}
}
请求行
用于query string / request parameter
package kim.nzxy.ly.common.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.time.format.DateTimeFormatter;
/**
* request parameter date time format
* @author ly-chn
*/
@Configuration
public class DateTimeFormatConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateTimeFormatter(dateTimeFormatter);
// 以下代码"可以, 但没必要"
registrar.setDateFormatter(DateTimeFormatter.ISO_LOCAL_DATE);
registrar.setTimeFormatter(DateTimeFormatter.ISO_LOCAL_TIME);
registrar.registerFormatters(registry);
}
}
yml方式配置, 任选其一即可
spring:
mvc:
format:
date-time: yyyy-MM-dd HH:mm:ss
# 以下代码"可以, 但没必要"
date: yyyy-MM-dd
time: HH:mm:ss
使用示例
实体类
package kim.nzxy.ly.test.entity;
import lombok.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@Data
public class Temp {
private LocalDateTime ldt;
private LocalDate ld;
private LocalTime lt;
}
controller中接收
package kim.nzxy.ly.controller;
import kim.nzxy.ly.modules.system.entity.Temp;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@RestController
@RequestMapping("temp")
public class TempController {
@GetMapping("1")
public Temp test1(LocalDateTime ldt, LocalDate ld, LocalTime lt) {
return new Temp(ldt, ld, lt);
}
@GetMapping("2")
public Temp test2(Temp temp) {
return temp;
}
@PostMapping("3")
public Temp test3(@RequestBody Temp temp) {
return temp;
}
}
test.rest
### 逐个传输
GET http://localhost:8937/temp/1?ldt=2006-01-02 15:04:05&ld=2006-01-02<=15:04
### 实体类接收
GET http://localhost:8937/temp/2?ldt=2006-01-02 15:04:05&ld=2006-01-02<=15:04
### json请求体
POST http://localhost:8937/temp/3
Content-Type: application/json
{
"ldt": "2006-01-02 15:04:05",
"ld": "2006-01-02",
"lt": "15:04"
}
总结
-
LocalTime和LocalDate的格式一般不用配置, 默认都是常用格式
-
DateTimeFormatter.ISO_LOCAL_TIME和DateTimeFormatter.ofPattern(String pattern)区别
这个可以查看其源码, ISO_LOCAL_TIME是可以解析"03:04" / "03:04:05" / "03:04:05.111222333"(时分, 时分秒, 时分秒+毫秒)这些格式的, 如果直接使用ofPattern则不行
-
Date类型怎么处理
首先不建议使用Date类型, 如果有需要, 尽量改成Local*, 除非后端只接受同一种日期格式:
-
yml配置参考上方yml方式配置
-
请求行配置尝试新建配置类, 继承WebBindingInitializer, 然后registerCustomEditor
-
Jackson yml
jackson: date-format: yyyy-MM-dd HH:mm:ss # Asia/Shanghai(CST) time-zone: GMT+8
-