java - 已解决 [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 个错误
问题描述
将我的航班信息保存到 mysql db 时出现错误。请帮助我下面是我的代码:
我已经通过添加模式和很多方法尝试了所有方法
它不会从字符串转换为日期
控制台日志
2020-05-12 13:19:21.040[0;39m [33m WARN[0;39m [35m5472[0;39m [2m---[0;39m [2m[nio-8080-exec-8][0;39m [36m.w.s.m.s.DefaultHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'flight' on field 'dateOfDeparture': rejected value [2020-05-20]; codes [typeMismatch.flight.dateOfDeparture,typeMismatch.dateOfDeparture,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [flight.dateOfDeparture,dateOfDeparture]; arguments []; default message [dateOfDeparture]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfDeparture'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-05-20'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'flight' on field 'estimatedDepartureTime': rejected value [2020-05-20]; codes [typeMismatch.flight.estimatedDepartureTime,typeMismatch.estimatedDepartureTime,typeMismatch.java.sql.Timestamp,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [flight.estimatedDepartureTime,estimatedDepartureTime]; arguments []; default message [estimatedDepartureTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'estimatedDepartureTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.sql.Timestamp] for value '2020-05-20'; nested exception is java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]]]
应用程序属性
spring.datasource.url=jdbc:mysql://localhost:3306/reservation?useSSL=false&serverTimeZone=IST&useLegacyDatetimeCode=false
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.show-sql=true
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/flight-reservation
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=*****
spring.mail.password=SunMoon@123
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
#Defined in logback.xml
logging.level.root=INFO
logging.file.name=D:/backup-Mahesh-Accion/logs/flight-reservation.log
com.accion.reservation.itenery.dirpath=C:/Users/Desktop/reservation/tickets
com.accion.reservation.itenery.email.body=Please find your attached flight booking tickets and Happy Journey! :)
com.accion.reservation.itenery.email.subject=Flight Booking Tickets Mahesh
FlightController.java
@Controller
public class FlightController {
@Autowired
private FlightRepository flightRepository;
@RequestMapping(value = "/saveFlight", method = RequestMethod.POST)
public String addFlight(@ModelAttribute @DateTimeFormat(pattern = "yyyy-MM-dd") Flight flight ) {
flightRepository.save(flight);
return "addFlight";
}
}
flight.java 实体类
Entity
public class Flight {
private String flightNumber;
private int id
private String operatingAirlines;
private String departureCity;
private String arrivalCity;
private Date dateOfDeparture;
private Timestamp estimatedDepartureTime;
//setters and getters
FlightRepository.java
@Repository
public interface FlightRepository extends JpaRepository<Flight, Long> {
}
addFlight.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add Flight</title>
</head>
<body>
<h2>Add Flight :</h2>
<form action="saveFlight" method="post">
FlightNumber :<input type="text" name="flightNumber"> </br>
Airlines Name: <input type="text" name="operatingAirlines"></br>
Departure City : <input type="text" name="departureCity"></br>
Arrival City: <input type="text" name="arrivalCity"></br>
Date Of Departure: <input type="date" name="dateOfDeparture"></br>
Estimated Date of Departure: <input type="date" name="estimatedDepartureTime"></br>
<input type="submit" value="Save Flight"></br>
</form>
</body>
</html>
解决方案
在类中使用@DateTimeFormat(pattern = "yyyy-MM-dd")
日期类型字段Flight
。当您使用日期数据时,例如:2020-05-20
为此estimatedDepartureTime
使用Date
类型。
public class Flight {
...
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateOfDeparture;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date estimatedDepartureTime;
}
推荐阅读
- c++ - 二进制“<<”:未找到采用“c1”类型的右侧操作数的运算符
- javascript - 从Javascript中的字符串中删除除数值之外的所有值
- node.js - 在 cPanel 主机上运行的节点应用程序在闲置 30 分钟后关闭
- python - Terraform 抱怨 lambda 中使用了列表
- python - 烧瓶在 send_file() 后无法删除文件
- firebase - 关于firestore更改的本地通知
- oracle - 在 JMeter 中测试存储过程 - 格式错误
- typo3 - TYPO3 11.5.2 静态模板“站点包”未显示
- ios - iOS:- 错误:无法创建存储目录。错误域 = NSCocoaErrorDomain 代码 = 512
- selenium - Google Analytics 错误:尝试使用 Selenium 和 ChromeOption Arg headless 在已分配的自定义维度错误中发送命中有效负载长度