Я пытался использовать API DateTimeFormatter, чтобы преобразовать строку в формат даты и запросить ее в интерфейсе репозитория.Тем не менее, я продолжаю получать ошибки в моем SearchController и не могу понять, почему.
SearchController:
@GetMapping("/search")
public String searchAccessInfoByDate(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate, Model model) {
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-mm-dd");
LocalDateTime date1 = LocalDate.parse(startDate, dateTimeFormatter1).atTime(LocalTime.MIN);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-mm-dd");
LocalDateTime date2 = LocalDate.parse(endDate, dateTimeFormatter2).atTime(LocalTime.MAX);
List<AccessInfo> foundAccessInfos = accessInfoRepository.searchAccessInfoByDate(date1, date2);
model.addAttribute("foundAccessInfos", foundAccessInfos);
return "search";
}
AccessInfoRepository:
@Repository
public interface AccessInfoRepository extends JpaRepository<AccessInfo, Long> {
@Query("select i from AccessInfo i where i.accessDate between ?1 and ?2")
List<AccessInfo> searchAccessInfoByDate(LocalDateTime startDate, LocalDateTime endDate);
}
AccessInfo.java (Entity):
package com.assignment.restapi.domain;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Date;
@Entity
public class AccessInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String accessedApi;
private Integer httpResponseStatusCode;
private Integer accessCount;
private Integer averageRequestTime;
private LocalDateTime accessDate;
public AccessInfo() {
}
public AccessInfo(String accessedApi, Integer httpResponseStatusCode, Integer accessCount, Integer averageRequestTime, LocalDateTime accessDate) {
this.accessedApi = accessedApi;
this.httpResponseStatusCode = httpResponseStatusCode;
this.accessCount = accessCount;
this.averageRequestTime = averageRequestTime;
this.accessDate = accessDate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAccessedApi() {
return accessedApi;
}
public void setAccessedApi(String accessedApi) {
this.accessedApi = accessedApi;
}
public Integer getHttpResponseStatusCode() {
return httpResponseStatusCode;
}
public void setHttpResponseStatusCode(Integer httpResponseStatusCode) {
this.httpResponseStatusCode = httpResponseStatusCode;
}
public Integer getAccessCount() {
return accessCount;
}
public void setAccessCount(Integer accessCount) {
this.accessCount = accessCount;
}
public Integer getAverageRequestTime() {
return averageRequestTime;
}
public void setAverageRequestTime(Integer averageRequestTime) {
this.averageRequestTime = averageRequestTime;
}
public LocalDateTime getAccessDate() {
return accessDate;
}
public void setAccessDate(LocalDateTime accessDate) {
this.accessDate = accessDate;
}
}
search.html (где отображается результат поиска):
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<form method="get" action="/search" style="padding: 10px 0">
<div>
<input type="date" name="startDate" th:value="startDate">
〜
<input type="date" name="endDate" th:value="endDate">
<input type="submit" value="検索">
</div>
</form>
<table border="1" th:if="${foundAccessInfos}">
<tr>
<th>APIエンドポイント</th>
<th>ステータスコード</th>
<th>アクセス回数[ns]</th>
<th>リクエスト平均時間</th>
<th>アクセスされた日付け</th>
</tr>
</table>
Текущая ошибка:
"Текст '2018-09-30' можетне обрабатывается: невозможно получить LocalDate от TemporalAccessor: {Year = 2018, MinuteOfHour = 9, DayOfMonth = 30}, ISO типа java.time.format.Parsed "]}