添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

JHipster generated file failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate'

Ask Question

I have generated the files by using JHipster and currently I am trying to call a query which involves a Date variable but am getting a failed conversion.

The following is my typescript file method which provide the criteria query for the search.

  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()

It then calls for the query method within the transaction.

  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)));

The following is the default JHipster convertDateArrayFromServer method that was provided.

  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;   

I have tried researching on methods to work around it and also tried out the following however the request level file that was generated from JHipster had a very different structure and I wasn't able to modify it accordingly to this webpage: https://www.baeldung.com/spring-date-parameters

Would kindly seek an assistance for a workable solution to the following error that was thrown.

"Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'transStartDate.equals'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value 'Fri Dec 03 2021 09:22:35 GMT 0800 (Singapore Standard Time)'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [Fri Dec 03 2021 09:22:35 GMT 0800 (Singapore Standard Time)]

The backend does not understand the JavaScript date string you are passing, the easiest solution is to use JHipster's date/time library of choice (dayjs or momentjs in older versions).

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

This should in theory return all the Transaction that have a transStartDate equal to the current date.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.