[已解决]JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String “12-21

时间选择器选择时间,传入后端报错

报错信息:

2023-12-21 13:15:27.683  WARN 11736 --- [nio-8090-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver 
: Resolved [org.springframework.http.converter.HttpMessageNotReadableException
: JSON parse error
: Cannot deserialize value of type `java.time.LocalDateTime` from String "12-21 13:14"
: Failed to deserialize java.time.LocalDateTime: 
(java.time.format.DateTimeParseException) Text '12-21 13:14' could not be parsed at index 0; 
nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException
: Cannot deserialize value of type `java.time.LocalDateTime` from String "12-21 13:14"
: Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '12-21 13:14' could not be parsed at index 0 at 
[Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 52] (through reference chain: com.example.memo.entity.Todo["time"])]

[已解决]JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String “12-21_第1张图片

报错原因:前端使用时间选择器,只传入了月、日、时、分,与后端LocalDateTime要求格式不一样,LocalDateTime必须按"yyyy-MM-dd HH:mm:ss"格式

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime time; 

解决方法:时间选择器年、月、日、时、分,秒全部传入至后端

data() {
	return {
		//时间选择器
		time:'',
		show:false,
		params: {
			    year: true,
			    month: true,
			    day: true,
				hour: true,
				minute: true,
				second: true
				},
			}
		},

//..method
//用于返回选择器选择的数据。
	confirmtime(e){
		console.log(e);
			//此处实现选择器值的接收
		this.time =  e.year + '-' + e.month + '-'+
			 e.day+' '+ e.hour + ':'+ e.minute + ':' + e.second;
		console.log(this.time);
	},

[已解决]JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String “12-21_第2张图片

[已解决]JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String “12-21_第3张图片

PS:有大佬知道如何解决只传入月、日、时、分吗?知道的大佬可以在评论区留言

你可能感兴趣的:(前后端分离,java,uni-app,spring,boot)