我在角7中有一个前端部分,在Java中有一个带有Spring框架的后端部分。我想在后端发布一个日期对象。后端有一个Local对象。我不需要LocalDateTime对象
我的约会服务是角质的。我需要保留日期类型,而不是使用字符串。
addDate(): Observable<Date> {
let now = new Date();
return this.http
.post<Date>('/api/date', now)
.pipe(
tap(response => {
return response;
}),
catchError(error => this.notificationService.handleError(error))
);
}我的后端服务:
@PostMapping
public LocalDate addIrregularity(@RequestBody LocalDate date, HttpServletRequest request) {
log.info(date);
return date;
}我有个错误:
2009-08-06 08:21:02.185警告1444年- nio-8080-exec-4 .w.s.m.s.DefaultHandlerExceptionResolver :已解析的.w.s.m.s.DefaultHandlerExceptionResolver JSON解析错误:无法从字符串“2019-08-06T00:00:00.000+0000”反序列化
java.time.LocalDate类型的值: java.time.LocalDate:(java.time.format.DateTimeParseException)文本'2019-08-06T00:00:00.000+0000‘无法解析,在索引23处找到未解析文本;嵌套异常是com.fasterxml.jackson.databind.exc.InvalidFormatException:不能从字符串“2019-0806T00:00:00.000+0000”反序列化java.time.LocalDate类型的值:无法反序列化java.time.LocalDate:(java.time.format.DateTimeParseException)文本‘2019-0806T00:00:00.000+0000’,索引23中未解析的文本
发布于 2021-03-03 16:11:08
如何将LocalDate和LocalDateTime从角度发送到弹簧
更好的解决办法:
app.module.ts
import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]app.service.ts
import { DatePipe } from '@angular/common';
constructor( private datePipe: DatePipe) {}
public sendStuff(): Observable<any>{
let params = new HttpParams().set("date", this.datePipe.transform(new Date(),"yyyy-MM-dd"))
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}弹簧部分
@PostMapping(path="values/show")
public Map<String, Object> showValues(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
@RequestParam("datetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime datetime) {) {
...
}如果您只想转换LocalDate,可以离开datePipe,只需从角发送新的Date().toISOString()。但是,由于发问者特别要求LocalDate,所以需要某种格式才能得到这种日期->“yyyy”。只有有了这个春天,才能发挥出它的魔力。
顺便说一句。如果您只需要时间(HH:mm:ss.SSSXXX),即DateTimeFormat.ISO.TIME,也是可能的。
完成!
不是很好的解决方案,更不舒服的->转换为String (没有测试,因为我对Spring解决方案很满意)
app.service.ts
public sendStuff(): Observable<any>{
let params = new HttpParams().set("datetime",new Date().toLocaleString())
.set("datetime",new Date().toISOString());
return this.http.post<any>("urlPath../values/show", "Body" , {
headers: new HttpHeaders({
'Accept': 'application/json'
}),
params
});
}春天:
@PostMapping(path="values/show")
public Map<String, Object> showValues( @RequestParam String date, @RequestParam String datetime) {
...
// code to transform String to Date copied somewhere
//Create a DateTimeFormatter with your required format:
DateTimeFormatter dateTimeFormat = new DateTimeFormatter(DateTimeFormatter.BASIC_ISO_DATE);
//Next parse the date from the @RequestParam, specifying the TO type as a TemporalQuery:
LocalDateTime date = dateTimeFormat.parse(datetime, LocalDateTime::from);
// some more code to transform LocalDate...
}https://stackoverflow.com/questions/57371807
复制相似问题