当我们直接想 mysql
传递 LocalDateTime
进行查询时,需要 mysql-connector
的版本符合一定的条件,如我上一篇java mybatis mysql使用LocalDateTime查询问题所示。
前端传后端:
当需要前端传递如"yyyy-MM-ddTHH:mm:ss"
的时间格式时,会发生Bad Request
:
Failed to convert value of type java.lang.String to required type java.time.LocalDate
这是因为前端传给我们的是字符串,参考文章,根据Failed to convert value of type ‘java.lang.String’ to required type ‘java.time.LocalDate’
这条信息来看,程序不能将一个String
类型的值转换成LocalDate
类型(前端传过来的是String
类型,程序会对这些数据进行相应的convert
,当匹配不到类型时,就会抛ConversionFailedException
异常)。那么就按照提示,补充相应的Converter
就好了。
一、LocalDateTime
的Converter
我们自定义的Converter
需要实现org.springframework.core.convert.converter.Converter
接口,这种方式是针对全局生效的,如果只是针对某个接口,可以使用 @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
这样的方式。
字符串转换成LocalDateTime
public class String2LocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String s) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
return LocalDateTime.parse(s, fmt);
}
}
二、注册Converter
这里我们选择通过WebMvcConfigurationSupport
的addFormatters(FormatterRegistry registry)
方法将自定义的Converter
注册到容器里面去
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addFormatters(FormatterRegistry registry) {
registry.addConverter(new String2LocalDateTimeConverter());
}
我们打开swagger
,然而发生异常:
No mapping for GET /swagger-ui.html
参考文章,遇到这种情况请先查找,最近是否添加继承了WebMvcConfigurationSupport
的类
如果继承了WebMvcConfigurationSupport
,则在配置文件在中配置的相关内容会失效,需要重新指定静态资源
import com.fii.amms.utils.String2LocalDateTimeConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
因此最终添加的代码如下:
LocalDateTime
的Converter
部分:
public class String2LocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String s) {
//注意下面T要加''
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
return LocalDateTime.parse(s, fmt);
}
}
添加注册容器及重新指定静态资源:
import com.utils.String2LocalDateTimeConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addFormatters(FormatterRegistry registry) {
registry.addConverter(new String2LocalDateTimeConverter());
}
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
// 如果使用SpringBoot集成swagger-bootstrap-ui后,无法访问doc.html界面
// 则需要添加如下:
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
super.addResourceHandlers(registry);
}
}
后端传前端
后端传给前端的格式也不是 “yyyy-MM-dd’T’HH:mm:ss”
的格式,而是类似数组格式:
[yyyy,MM,dd,HH,mm,ss]
感觉不太统一
注解@JsonFormat主要是后台到前台的时间格式的转换
注解@DataFormAT主要是前后到后台的时间格式的转换
因此通过在传给前端的实体类上加注解: @JsonFormat(pattern = “yyyy-MM-dd’T’HH:mm:ss”)
即可
Excel
导入导出
需要指定LocalDateTime
的Converter
,如下所示,并在需要导出Excel
的实体类LocalDateTime
上添加Converter
的注解:
@ExcelProperty(value = "创建时间", converter = LocalDateTimeConverter.class)
private LocalDateTime createTime;
public class LocalDateTimeConverter implements Converter<LocalDateTime> {
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public Class supportJavaTypeKey() {
return null;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return null;
}
@Override
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DEFAULT_PATTERN);
return LocalDateTime.parse(cellData.getStringValue(), dateTimeFormatter);
}
@Override
public CellData convertToExcelData(LocalDateTime localDateTime, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData<>(dateTimeFormatter.format(localDateTime));
}
}
参考文章:
Failed to convert value of type java.lang.String to required type java.time.LocalDate
No mapping for GET /swagger-ui.html
@JsonFormat与@DateTimeFormat注解的使用
【转】集成 swagger-bootstrap-ui后访问 doc.html页面404
EasyExcel导入excel中时间格式到LocalDateTime字段转换器Converter