客户端传日期格式字段(String),服务端接口使用java.util.Date类型接收报错问题

问题演示

  1. 演示代码

服务端接口代码

	@PostMapping("/binder")
	@ResponseBody
	public String binderTest(TestEntity te) {
		return te.getBirthDay().toString() ;
	}

以上接口中的实体TestEntity

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Data;

@Data
public class TestEntity {

	private String name;
	
	private String addr;
	
	private Date birthDay;
}

TestEntity中的字段birthDay为Date类型

客户端演示使用PostMan

第1种:客户端以URL拼接的方式传值

在这里插入图片描述
后台报错:

Field error in object 'testEntity' on field 'birthDay': rejected value [2024-02-09 22:22:33]; codes 
[typeMismatch.testEntity.birthDay,typeMismatch.birthDay,typeMismatch.java.util.Date,typeMismatch]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes 
[testEntity.birthDay,birthDay]; arguments []; default message [birthDay]]; default message [Failed to convert 
property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthDay'; nested exception 
is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] 
to type [java.util.Date] for value '2024-02-09 22:22:33'; nested exception is 
java.lang.IllegalArgumentException]]

第2种:客户端以body中的form-data方式提交

在这里插入图片描述
后台报错:

Field error in object 'testEntity' on field 'birthDay': rejected value [2024-02-07]; codes 
[typeMismatch.testEntity.birthDay,typeMismatch.birthDay,typeMismatch.java.util.Date,typeMismatch]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes 
[testEntity.birthDay,birthDay]; arguments []; default message [birthDay]]; default message [Failed to convert 
property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthDay'; nested exception 
is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] 
to type [java.util.Date] for value '2024-02-07'; nested exception is java.lang.IllegalArgumentException]]

第3种 客户端以Body中的json方式提交

这里需要先在接口中,添加注解@RequestBody,接口变成如下:

	@PostMapping("/binder")
	@ResponseBody
	public String binderTest(@RequestBody TestEntity te) {
		return te.getBirthDay().toString() ;
	}

在这里插入图片描述
以上日期格式是 yyyy-MM-dd (2024-06-08),可以成功!
但是,将格式变成yyyy-MM-dd HH:mm:ss,就不行了,见如下:
在这里插入图片描述

后台报错:

JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2024-06-08 22:11:33": not a 
valid representation (error: Failed to parse Date value '2024-06-08 22:11:33': Cannot parse date "2024-06-08 
22:11:33": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested 
exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type 
`java.util.Date` from String "2024-06-08 22:11:33": not a valid representation (error: Failed to parse Date value 
'2024-06-08 22:11:33': Cannot parse date "2024-06-08 22:11:33": while it seems to fit format 'yyyy-MM-
dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))
 at [Source: (PushbackInputStream); line: 2, column: 16] (through reference chain: 

问题解决(全局解决方式)

针对 第1和第2种情况

解决办法

新增日期转换类,并将其纳入到spring的bean管理中:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

/**
 * 日期格式转换类,
 * 
 * 仅针对当客户端是以下两种方式日期格式值的转换
 * 
 * 1.url地址拼接的方式,形如:localhost:8031/binder?birthDay=2024-02-09 22:22:33。
 * 
 * 2.body方式中的form-data方式!
 * 
 * 注意!!!与DateJacksonConverter类区别。
 * 
 * @author Administrator
 */
public class MyDateConverter implements Converter<String, Date> {

	// TODO 2024年4月12日16:01:01
	// 完善—日期格式有多种这里只列举了一种,根据传入的String格式日期分别初始化SimpleDateFormat,还有如下格式需要处理:
	//private static String[] pattern = new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss",
	//		"yyyy-MM-dd HH:mm:ss.S", "yyyy.MM.dd", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm:ss.S",
	//		"yyyy/MM/dd", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss.S" };

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public Date convert(String s) {
		Date date = null;
		try {
			date = sdf.parse(s);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return date;
	}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 配置数据绑定
 * 
 * @author Administrator
 *
 */
@Configuration
public class MyConfigurableWebBindingInitializer {

	/**
	 * 仅针对当客户端是以下两种方式日期格式值的转换
	 * 1.url地址拼接的方式,形如:localhost:8031/binder?birthDay=2024-02-09 22:22:33。
	 * 2.body方式中的form-data方式!
	 * 
	 * @return
	 */
	@Bean
	public MyDateConverter myDateConverter() {
		return new MyDateConverter();
	}
}

验证

在这里插入图片描述
在这里插入图片描述
均成功!

针对第3中情况

解决办法

同样,新增日期转换类,并将其纳入到spring的bean管理中:

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

/**
 * 日期格式转化类:
 * 
 * 针对客户端传值方式为 body中的json方式
 * 
 * 注意!!!与MyDateConverter类区别。
 * 
 * @author Administrator
 * 
 * https://www.jianshu.com/p/c97a20fc9a35
 *
 */
public class DateJacksonConverter extends JsonDeserializer<Date> {
	private static String[] pattern = new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss",
			"yyyy-MM-dd HH:mm:ss.S", "yyyy.MM.dd", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm:ss.S",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss.S" };

	@Override
	public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {

		Date targetDate = null;
		String originDate = p.getText();
		if (StringUtils.isNotEmpty(originDate)) {
			try {
				long longDate = Long.valueOf(originDate.trim());
				targetDate = new Date(longDate);
			} catch (NumberFormatException e) {
				try {
					targetDate = DateUtils.parseDate(originDate, DateJacksonConverter.pattern);
				} catch (ParseException pe) {
					throw new IOException(String.format(
							"'%s' can not convert to type 'java.util.Date',just support timestamp(type of long) and following date format(%s)",
							originDate, StringUtils.join(pattern, ",")));
				}
			}
		}
		return targetDate;
	}

	@Override
	public Class<?> handledType() {
		return Date.class;
	}
}


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 配置数据绑定
 * 
 * @author Administrator
 *
 */
@Configuration
public class MyConfigurableWebBindingInitializer {

	/**
	 * 仅针对当客户端是以下两种方式日期格式值的转换
	 * 1.url地址拼接的方式,形如:localhost:8031/binder?birthDay=2024-02-09 22:22:33。
	 * 2.body方式中的form-data方式!
	 * 
	 * @return
	 */
	@Bean
	public MyDateConverter myDateConverter() {
		return new MyDateConverter();
	}

	// ======针对客户端传值方式为 body中的json方式:对日期格式进行转换======开始
	@Bean
	public DateJacksonConverter dateJacksonConverter() {
		return new DateJacksonConverter();
	}

	@Bean
	public Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean(
			@Autowired DateJacksonConverter dateJacksonConverter) {
		Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean();

		jackson2ObjectMapperFactoryBean.setDeserializers(dateJacksonConverter);
		return jackson2ObjectMapperFactoryBean;
	}

	@Bean
	public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(
			@Autowired ObjectMapper objectMapper) {
		MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
		mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
		return mappingJackson2HttpMessageConverter;
	}
	// ======针对客户端传值方式为 body中的json方式======结束

}

验证

需给接口加上@RequestBody注解,略。
在这里插入图片描述
成功!

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MyBatis中,当你在查询条件中将`java.util.Date`类型的属性与`java.lang.String`类型的属性进行比较时,可能会出现报错信息"invalid comparison: java.util.Date and java.lang.String"。这是因为在比较过程中,MyBatis无法将`java.util.Date`类型的属性与`java.lang.String`类型的属性进行正确的比较。 为了解决这个问题,你可以使用MyBatis提供的类型处理器来处理`java.util.Date`类型的属性。类型处理器可以将`java.util.Date`类型的属性转换为数据库中的日期类,以便正确比较。 以下是一个示例,展示了如何在MyBatis中使用类型处理器来解决`java.util.Date`和`java.lang.String`比较的问题: 1. 首先,在你的MyBatis配置文件中,添加类型处理器的配置: ```xml <typeHandlers> <typeHandler handler="org.apache.ibatis.type.DateTypeHandler" /> </typeHandlers> ``` 2. 然后,在你的Mapper接口中,将`java.util.Date`类型的属性与`java.lang.String`类型的属性进行比较: ```xml <select id="selectByDateAndString" parameterType="map" resultType="YourResultType"> SELECT * FROM your_table WHERE date_column = #{dateProperty, jdbcType=DATE} AND string_column = #{stringProperty, jdbcType=VARCHAR} </select> ``` 在上面的示例中,`date_column`是数据库表中的日期类列,`string_column`是数据库表中的字符串类型列。`dateProperty`和`stringProperty`是你入的参数,分别对应`java.util.Date`类型的属性和`java.lang.String`类型的属性。 通过使用类型处理器和正确设置jdbcType,你可以避免在MyBatis中比较`java.util.Date`和`java.lang.String`类型时出现报错

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值