繁体   English   中英

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

[英]JHipster generated file failed to convert property value of type 'java.lang.String' to required type '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' 类型的属性值转换为属性 'transStartDate.equals' 所需的类型 'java.time.LocalDate';嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从键入 [java.lang.String] 以键入 [java.time.LocalDate] 值'Fri Dec 03 2021 09:22:35 GMT 0800 (Singapore Standard Time)';嵌套异常是 java.lang.IllegalArgumentException:解析尝试失败价值 [2021 年 12 月 3 日星期五 09:22:35 GMT 0800(新加坡标准时间)]

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

import * as dayjs from 'dayjs';
...
  loadSearchPage(page?: number): void {
    const pageToLoad: number = page || this.page;

    this.transactionService
      .query({
        page: pageToLoad - 1,
        size: this.itemsPerPage,
        sort: this.sort(),
        'transStartDate.equals': dayjs()
      })
      .subscribe(
        (res: HttpResponse<ITransaction[]>) => this.onSuccess(res.body, res.headers, pageToLoad),
        () => this.onError()
      );
  }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM