Java 如何从angular接收日期(spring)类型的requestParam
我是个有角度和弹簧的新手 我正试图发送一个带有一些日期参数的get请求,但我一直收到一个错误Java 如何从angular接收日期(spring)类型的requestParam,java,angular,spring,spring-boot,spring-mvc,Java,Angular,Spring,Spring Boot,Spring Mvc,我是个有角度和弹簧的新手 我正试图发送一个带有一些日期参数的get请求,但我一直收到一个错误 下面是html代码: 这里是角度服务代码 这里是spring控制器代码 ps:我已经尝试了stackoverflow的一些解决方案,但没有任何效果,在您的Angular service中,您将搜索的日期作为它们的toString()表示发送。但是在Spring控制器中,您希望它们是“dd/mm/yyyy”格式。Spring无法将这些日期字符串解析为date,因此出现了IllegalArgum
- 下面是html代码:
- 这里是角度服务代码
- 这里是spring控制器代码
ps:我已经尝试了stackoverflow的一些解决方案,但没有任何效果,在您的Angular service中,您将
搜索的日期作为它们的toString()
表示发送。但是在Spring控制器中,您希望它们是“dd/mm/yyyy”格式。Spring无法将这些日期字符串解析为date
,因此出现了IllegalArgumentException
您可以在Angular service中将JS日期格式化为“dd/mm/yyyy”。例如,使用以下简单方法:
formatDate(date: Date): string {
const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate() + '';
const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : (date.getMonth() + 1) + '';
const year = date.getFullYear() + '';
return `${day}/${month}/${year}`;
}
最后,您需要将@DateTimeFormat
的模式设置为正确(mm
为小时分钟)
@GetMapping(“/cheques”)
公众反应(
@RequestParam(name=“beforeaccessment”,defaultValue=“01/01/1950”)@DateTimeFormat(pattern=“dd/MM/yyyy”)入院前的日期,
@RequestParam(name=“afterAdmission”,defaultValue=“01/01/2050”)@DateTimeFormat(pattern=“dd/MM/yyyy”)入院后的日期,
@RequestParam(name=“beforePay”,defaultValue=“01/01/1950”)@DateTimeFormat(pattern=“dd/MM/yyyy”)支付前的日期,
@RequestParam(name=“afterPay”,defaultValue=“01/01/2050”)@DateTimeFormat(pattern=“dd/MM/yyyy”)日期afterPay){
系统输出打印项次(“xx”);
回程服务。回程((日期)入院前,(日期)入院后,(日期)支付前,(日期)支付后);
}
我还建议使用java.time.LocalDate
overjava.util.Date
在您的Angular服务中,您将搜索的日期作为它们的toString()
表示形式发送。但是在Spring控制器中,您希望它们是“dd/mm/yyyy”格式。Spring无法将这些日期字符串解析为date
,因此出现了IllegalArgumentException
您可以在Angular service中将JS日期格式化为“dd/mm/yyyy”。例如,使用以下简单方法:
formatDate(date: Date): string {
const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate() + '';
const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : (date.getMonth() + 1) + '';
const year = date.getFullYear() + '';
return `${day}/${month}/${year}`;
}
最后,您需要将@DateTimeFormat
的模式设置为正确(mm
为小时分钟)
@GetMapping(“/cheques”)
公众反应(
@RequestParam(name=“beforeaccessment”,defaultValue=“01/01/1950”)@DateTimeFormat(pattern=“dd/MM/yyyy”)入院前的日期,
@RequestParam(name=“afterAdmission”,defaultValue=“01/01/2050”)@DateTimeFormat(pattern=“dd/MM/yyyy”)入院后的日期,
@RequestParam(name=“beforePay”,defaultValue=“01/01/1950”)@DateTimeFormat(pattern=“dd/MM/yyyy”)支付前的日期,
@RequestParam(name=“afterPay”,defaultValue=“01/01/2050”)@DateTimeFormat(pattern=“dd/MM/yyyy”)日期afterPay){
系统输出打印项次(“xx”);
回程服务。回程((日期)入院前,(日期)入院后,(日期)支付前,(日期)支付后);
}
我还建议在日志中使用java.time.LocalDate
overjava.util.Date
,看起来您将JS日期作为其toString()
发送,而不是按照@DateTimeFormat
中定义的预期模式发送。请共享您的Angular服务和组件代码。@drumonii我编辑了代码并添加了Angular component+服务,感谢您在日志中的注释,看起来您是将JS日期作为它的发送到字符串()
,而不是按照您在@DateTimeFormat
中定义的预期模式发送的。请分享您的角度服务和组件代码。@drumonii我编辑了代码并添加了角度组件+服务,谢谢您的注释
public search(search){
return this.http.get(this.url+"?beforeAdmission="+search.beforeAdmission+"&afterAdmission="+search.afterAdmission
+"&beforePay="+search.beforePay+"&afterPay="+search.afterPay,{headers:this.headers});
}```
@GetMapping("/cheques")
public Page<ChequeOrEffet> getAll(@RequestParam(name="beforeAdmission",defaultValue = "01/01/1950")@DateTimeFormat(pattern = "dd/mm/yyyy",iso=ISO.DATE)Date beforeAdmission
,@RequestParam(name="afterAdmission",defaultValue = "01/01/2050") @DateTimeFormat(pattern = "dd/mm/yyyy",iso=ISO.DATE) Date afterAdmission
,@RequestParam(name="beforePay",defaultValue = "01/01/1950") @DateTimeFormat(pattern = "dd/mm/yyyy",iso=ISO.DATE) Date beforePay
,@RequestParam(name="afterPay",defaultValue = "01/01/2050") @DateTimeFormat(pattern = "dd/mm/yyyy",iso=ISO.DATE) Date afterPay) {
System.out.println("xx");
return service.seach((Date)beforeAdmission, (Date)afterAdmission, (Date)beforePay, (Date)afterPay);
}
2020-08-31 01:50:45.158 WARN 35616 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value 'Sun Aug 31 2025 00:00:00 GMT 0000 (Coordinated Universal Time)'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [Sun Aug 31 2025 00:00:00 GMT 0000 (Coordinated Universal Time)]]
formatDate(date: Date): string {
const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate() + '';
const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : (date.getMonth() + 1) + '';
const year = date.getFullYear() + '';
return `${day}/${month}/${year}`;
}
public search(search) {
const params = new HttpParams()
.set('beforeAdmission', formatDate(search.beforeAdmission))
.set('afterAdmission', formatDate(search.afterAdmission))
.set('beforePay', formatDate(search.beforePay))
.set('afterPay', formatDate(search.afterPay));
const options = {
params,
headers: this.headers
};
return this.http.get(this.url, options);
}
@GetMapping("/cheques")
public ResponseEntity<String> getAll(
@RequestParam(name = "beforeAdmission", defaultValue = "01/01/1950") @DateTimeFormat(pattern = "dd/MM/yyyy") Date beforeAdmission,
@RequestParam(name = "afterAdmission", defaultValue = "01/01/2050") @DateTimeFormat(pattern = "dd/MM/yyyy") Date afterAdmission,
@RequestParam(name = "beforePay", defaultValue = "01/01/1950") @DateTimeFormat(pattern = "dd/MM/yyyy") Date beforePay,
@RequestParam(name = "afterPay", defaultValue = "01/01/2050") @DateTimeFormat(pattern = "dd/MM/yyyy") Date afterPay) {
System.out.println("xx");
return service.seach((Date) beforeAdmission, (Date) afterAdmission, (Date) beforePay, (Date) afterPay);
}