spring boot 统一处理时间参数
在网上面搜了很多都发现有点没有描述清楚应该分两种情况
1.对于非请求体json参数(url参数,表单参数)
我选择的是添加一个时间转换器
package com.htsc.thfx.framework.interceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * @ClassName: InterceptorConfig * @Description: TODO * @author: K0180041 * @date: 2018年7月25日 下午6:51:48 */ @Configuration public class WebMvcConfigurerConfig extends WebMvcConfigurerAdapter { @Override public void addFormatters(FormatterRegistry registry) { // lambda 会报错 registry.addConverter(new Converter<String, Date>() { @Override public Date convert(String stringDate) { SimpleDateFormat simpleDateFormat; if (stringDate.contains(" ")) { simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } else { simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); } try { return simpleDateFormat.parse(stringDate); } catch (ParseException e) { e.printStackTrace(); } return null; } }); } }
2.对于请求体json参数
在时间类型添加暂时没有找到合适的全局处理的方式,思路应该是修改json的序列化的方式......
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")