Spring Boot Search Data by Date (formated in ZonedDateTime)

南笙酒味 提交于 2019-12-11 07:22:37

问题


I want to get list of forms submitted on a particular date. Date will be passed by Admin in Search box (from front end it will be String but in Back end it's ZonedDateTime).

I've written method in Repository as:

List<AdmissionForm> findByDateContains(ZonedDateTime datePart);

method formation ref : https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-creating-database-queries-from-method-names/

My Service class background logic is like:

@Transactional(readOnly = true)
public List<AdmissionFormDTO> findByDate(ZonedDateTime datepart){
    return admissionFormRepository.findByDateContains(datepart).stream()
        .map(admissionFormMapper::toDto)
        .collect(Collectors.toList());
}

And My Resource endpoint is like:

@GetMapping("/admissionForms/date")
@Timed
public List<AdmissionFormDTO> findByDate(@RequestParam("date") String date){
    log.debug("REST request to get AdmissionForms by searchTerm :" + date);
    //String to ZonedDateTime method conversion from seperate class
    ZonedDateTime zDate = SupportUtils.convertStringDateToZoneDateTime(date);
    return admissionFormService.findByDate(zDate);
}

I've tried with String search terms and it's working fine but got only trouble with Searching with Date.

I got the exception like:

java.lang.IllegalArgumentException: Parameter value [%2018-11-20T00:00Z%] did not match expected type [java.time.ZonedDateTime (n/a)]' and exception = 'Parameter value [%2018-11-20T00:00Z%] did not match expected type [java.time.ZonedDateTime (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [%2018-11-20T00:00Z%] did not match expected type [java.time.ZonedDateTime (n/a)]'

Give some corrections so I can do search by Date as expected.

EDITED: I changed datatype from ZoneDateTime to LocalDate and solve accordingly as below (Skip timing for now):

My Repository code:

List<AdmissionForm> findByDate(LocalDate datePart);

My Service Class:

@Transactional(readOnly = true)
public List<AdmissionFormDTO> findByDate(String datepart){
    LocalDate localDatePart = SupportUtils.convertStringDateToLocalDate(datepart);
    return admissionFormRepository.findByDate(localDatePart).stream()
        .map(admissionFormMapper::toDto)
        .collect(Collectors.toList());
}

My Resource Entry Point:

@GetMapping("/admissionForms/date")
@Timed
public List<AdmissionFormDTO> findByDate(@RequestParam("date")String date){
    log.debug("REST request to get AdmissionForms by searchTerm :{}",date);
    return admissionFormService.findByDate(date);
}

Thanks for your suggestions, but can't find a way to work with ZondDateTime so we change requirements little bit. Hope others will get help from question and suggested answers here.


回答1:


Use DateTimeFormat annotation which by default handles the ISO yyyy-MM-dd'T'HH:mm:ss.SSSZ date time format:

public List<AdmissionFormDTO> findByDate(
          @RequestParam("date") @DateTimeFormat ZonedDateTime date) {
  return admissionFormService.findByDate(date);
}

You should further customize @DateTimeFormat if your date doesn't contain seconds fields as in your example.



来源:https://stackoverflow.com/questions/53317625/spring-boot-search-data-by-date-formated-in-zoneddatetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!