按日期搜索 Web 方法的问题使用 GetMapping
Problem with search by date web method use GetMapping
我有 web-rest Spring 应用程序。对于第二个实体,我有 2 个带有 LocalDate 的字段:arrivalTime、departmentTime。
我实现了 CRUD 方法。没关系。但是当我尝试按日期(从到达时间到出发时间)搜索时,没有任何反应。刚刚在浏览器中加载。
我的实体:
@NotNull(message = "arrival time is a required field")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate arrivalTime;
@NotNull(message = "departure time is a required field")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate departureTime;
public Resident() {
}
public Resident(String firstName, String lastName, String email, LocalDate arrivalTime, LocalDate departureTime, Integer apartmentNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.arrivalTime = arrivalTime;
this.departureTime = departureTime;
this.apartmentNumber = apartmentNumber;
}
我的网络控制器
@GetMapping("/search")
public String searchAllResidentByDate(@RequestParam(name = "arrivalTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate arrivalTime,
@RequestParam(name = "departureTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate departureTime,
Model model) {
LOGGER.debug("search residents by date() {} {}", arrivalTime, departureTime);
List<Resident> residentListByTime = residentService.findAllByTime(arrivalTime, departureTime);
model.addAttribute("allResidentsAttribute", residentListByTime);
return "Residents_list";
}
服务休息:
@Override
public List<Resident> findAllByTime(LocalDate arrivalTime, LocalDate departureTime) {
String arrivalTimeString = arrivalTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String departureTimeString = departureTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String searchUrl = "http://localhost:8080/search?arrivalTime=" + arrivalTimeString + "&departureTime=" + departureTimeString;
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<Apartment> entity = new HttpEntity<>(headers);
return restTemplate.exchange(searchUrl, HttpMethod.GET, entity, new ParameterizedTypeReference<List<Resident>>() {
}).getBody();
}
Html部分:
<form class="d-flex"
action="/search"
th:method="@{get}">
<div class="col-sm-2">
<input class="form-control form-control-sm" name="arrivalTime" type="date" aria-label="arrivalTime"
id="arrivalTime">
</div>
<div class="col-sm-2">
<input class="form-control form-control-sm" name="departureTime" type="date" aria-label="departureTime"
id="departureTime">
</div>
<button type="submit" class="btn btn-outline-secondary btn-sm">Search</button>
</form>
我的网络模块 POM:
<dependency>
<groupId>com.punko</groupId>
<artifactId>model</artifactId>
</dependency>
<dependency>
<groupId>com.punko</groupId>
<artifactId>service-api</artifactId>
</dependency>
<dependency>
<groupId>com.punko</groupId>
<artifactId>service-rest</artifactId>
</dependency>
<dependency>
<groupId>com.punko</groupId>
<artifactId>test-db</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
您的 REST 服务尝试向 MVC 端点请求 returns HTML 字符串,而不是居民列表。在 CRUD 中实现 findAllByTime,而不是 REST 请求。
您可能有兴趣使用这个库:https://github.com/turkraft/spring-filter
它将让您 运行 搜索查询,例如:
/search?filter= average(ratings) > 4.5 and brand.name in ('audi', 'land rover') and (year > 2018 or km < 50000) and color : 'white' and accidents is empty
它支持枚举、日期和集合等多种字段类型。
谢谢,我发现了一个错误。我需要使用 8090 端口而不是 8080。
我有 web-rest Spring 应用程序。对于第二个实体,我有 2 个带有 LocalDate 的字段:arrivalTime、departmentTime。 我实现了 CRUD 方法。没关系。但是当我尝试按日期(从到达时间到出发时间)搜索时,没有任何反应。刚刚在浏览器中加载。
我的实体:
@NotNull(message = "arrival time is a required field")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate arrivalTime;
@NotNull(message = "departure time is a required field")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate departureTime;
public Resident() {
}
public Resident(String firstName, String lastName, String email, LocalDate arrivalTime, LocalDate departureTime, Integer apartmentNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.arrivalTime = arrivalTime;
this.departureTime = departureTime;
this.apartmentNumber = apartmentNumber;
}
我的网络控制器
@GetMapping("/search")
public String searchAllResidentByDate(@RequestParam(name = "arrivalTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate arrivalTime,
@RequestParam(name = "departureTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate departureTime,
Model model) {
LOGGER.debug("search residents by date() {} {}", arrivalTime, departureTime);
List<Resident> residentListByTime = residentService.findAllByTime(arrivalTime, departureTime);
model.addAttribute("allResidentsAttribute", residentListByTime);
return "Residents_list";
}
服务休息:
@Override
public List<Resident> findAllByTime(LocalDate arrivalTime, LocalDate departureTime) {
String arrivalTimeString = arrivalTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String departureTimeString = departureTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String searchUrl = "http://localhost:8080/search?arrivalTime=" + arrivalTimeString + "&departureTime=" + departureTimeString;
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<Apartment> entity = new HttpEntity<>(headers);
return restTemplate.exchange(searchUrl, HttpMethod.GET, entity, new ParameterizedTypeReference<List<Resident>>() {
}).getBody();
}
Html部分:
<form class="d-flex"
action="/search"
th:method="@{get}">
<div class="col-sm-2">
<input class="form-control form-control-sm" name="arrivalTime" type="date" aria-label="arrivalTime"
id="arrivalTime">
</div>
<div class="col-sm-2">
<input class="form-control form-control-sm" name="departureTime" type="date" aria-label="departureTime"
id="departureTime">
</div>
<button type="submit" class="btn btn-outline-secondary btn-sm">Search</button>
</form>
我的网络模块 POM:
<dependency>
<groupId>com.punko</groupId>
<artifactId>model</artifactId>
</dependency>
<dependency>
<groupId>com.punko</groupId>
<artifactId>service-api</artifactId>
</dependency>
<dependency>
<groupId>com.punko</groupId>
<artifactId>service-rest</artifactId>
</dependency>
<dependency>
<groupId>com.punko</groupId>
<artifactId>test-db</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
您的 REST 服务尝试向 MVC 端点请求 returns HTML 字符串,而不是居民列表。在 CRUD 中实现 findAllByTime,而不是 REST 请求。
您可能有兴趣使用这个库:https://github.com/turkraft/spring-filter
它将让您 运行 搜索查询,例如:
/search?filter= average(ratings) > 4.5 and brand.name in ('audi', 'land rover') and (year > 2018 or km < 50000) and color : 'white' and accidents is empty
它支持枚举、日期和集合等多种字段类型。
谢谢,我发现了一个错误。我需要使用 8090 端口而不是 8080。