JHipster生成的文件无法将类型'java.lang.String'的属性值转换为所需的类型'java.time.LocalDate'



我已经使用JHipster生成了文件,目前我正在尝试调用一个涉及Date变量的查询,但转换失败。

下面是我的typescript文件方法,它为搜索提供了条件查询。

loadSearchPage(page?: number): void {
const pageToLoad: number = page || this.page;
this.transactionService
.query({
page: pageToLoad - 1,
size: this.itemsPerPage,
sort: this.sort(),
'transStartDate.equals': new Date()
})
.subscribe(
(res: HttpResponse<ITransaction[]>) => this.onSuccess(res.body, res.headers, pageToLoad),
() => this.onError()
);
}

然后它在事务中调用查询方法。

query(req?: any): Observable<EntityArrayResponseType> {
const options = createRequestOption(req);
return this.http
.get<ITransaction[]>(this.resourceUrl, { params: options, observe: 'response' })
.pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
}

以下是提供的默认JHipster convertDateArrayFromServer方法。

protected convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
if (res.body) {
res.body.forEach((transaction: ITransaction) => {
transaction.transStartDate = transaction.transStartDate ? moment(transaction.transStartDate) : undefined;
transaction.transEndDate = transaction.transEndDate ? moment(transaction.transEndDate) : undefined;
});
}
return res;   
}

我尝试过研究解决它的方法,也尝试过以下方法,但JHipster生成的请求级文件的结构非常不同,我无法根据此网页对其进行相应的修改:https://www.baeldung.com/spring-date-parameters

对于引发的以下错误,我们将寻求一个可行的解决方案。

"未能将"java.lang.String"类型的属性值转换为属性的必需类型"java.time.LocalDate"'transStartDate.equals';嵌套异常为org.springframework.core.covert.ConversionFailedException:未能将的类型[java.lang.String]转换为类型[java.time.LocalDate]value"2021年12月3日星期五09:22:35 GMT 0800(新加坡标准时间(";嵌套异常为java.lang.IollegalArgumentException:分析尝试价值失败[2021年12月3日星期五09:22:35 GMT 0800(新加坡标准时间(]

后端不理解您传递的JavaScript日期字符串,最简单的解决方案是使用JHipster的日期/时间库(在旧版本中为dayjs或momentjs(。

import * as moment from 'moment';
// ...
'transStartDate.equals': moment()
// ...

或者:

import * as dayjs from 'dayjs';
// ...
'transStartDate.equals': dayjs()
// ...

理论上,这应该返回具有等于当前日期的transStartDate的所有Transaction

相关内容

最新更新