无法将类型 'java.lang.String' 的值转换为所需类型 'java.time.LocalDate'
Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'
我有 web-rest Spring 应用程序。对于第二个实体,我有 2 个带有 LocalDate 的字段:arrivalTime、departmentTime。
我实现了 CRUD 方法。没关系。但是当我尝试按日期搜索时(从到达时间到出发时间)我有一个错误:
Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
我的休息模块答案正确。我不知道该怎么办。
我的实体:
@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(iso = DateTimeFormat.ISO.DATE) LocalDate arrivalTime,
@RequestParam(name = "departureTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) 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 searchUrl = "http://localhost:8080/search";
String arrivalTimeString = "{arrivalTime}";
String departureTimeString = "{departureTime}";
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(searchUrl).queryParam("arrivalTime", arrivalTimeString)
.queryParam("departureTime", departureTimeString);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<Apartment> entity = new HttpEntity<>(headers);
return restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, new ParameterizedTypeReference<List<Resident>>() {
}).getBody();
}
休息控制器:
@GetMapping("/search")
public ResponseEntity<List<Resident>> searchAllResidentByDate(
@RequestParam("arrivalTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate arrivalTime,
@RequestParam("departureTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate departureTime) {
List<Resident> residents = residentService.findAllByTime(arrivalTime, departureTime);
LOGGER.debug("Find residents by date, where arrivalTime = {} and end departureTime => {} In the amount of {} ", arrivalTime, departureTime, residents.size());
return new ResponseEntity<>(residents, HttpStatus.FOUND);
}
通过这个 URL:
我得到了正确的休息结果
http://localhost:8090/search?arrivalTime=2021-03-01&departureTime=2021-04-04
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>
当我点击搜索按钮时,我有这个 URL:
http://localhost:8080/search?arrivalTime=2021-03-01&departureTime=2021-05-09
I need this pattern:yyyy-MM-dd. But something doesn't work.
我的网络模块 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>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</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.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.4</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.punko.web_app.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
我使用了错误的端口。我需要 8090,而不是 8080。谢谢
我有 web-rest Spring 应用程序。对于第二个实体,我有 2 个带有 LocalDate 的字段:arrivalTime、departmentTime。 我实现了 CRUD 方法。没关系。但是当我尝试按日期搜索时(从到达时间到出发时间)我有一个错误:
Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
我的休息模块答案正确。我不知道该怎么办。 我的实体:
@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(iso = DateTimeFormat.ISO.DATE) LocalDate arrivalTime,
@RequestParam(name = "departureTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) 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 searchUrl = "http://localhost:8080/search";
String arrivalTimeString = "{arrivalTime}";
String departureTimeString = "{departureTime}";
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(searchUrl).queryParam("arrivalTime", arrivalTimeString)
.queryParam("departureTime", departureTimeString);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<Apartment> entity = new HttpEntity<>(headers);
return restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, new ParameterizedTypeReference<List<Resident>>() {
}).getBody();
}
休息控制器:
@GetMapping("/search")
public ResponseEntity<List<Resident>> searchAllResidentByDate(
@RequestParam("arrivalTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate arrivalTime,
@RequestParam("departureTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate departureTime) {
List<Resident> residents = residentService.findAllByTime(arrivalTime, departureTime);
LOGGER.debug("Find residents by date, where arrivalTime = {} and end departureTime => {} In the amount of {} ", arrivalTime, departureTime, residents.size());
return new ResponseEntity<>(residents, HttpStatus.FOUND);
}
通过这个 URL:
我得到了正确的休息结果http://localhost:8090/search?arrivalTime=2021-03-01&departureTime=2021-04-04
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>
当我点击搜索按钮时,我有这个 URL:
http://localhost:8080/search?arrivalTime=2021-03-01&departureTime=2021-05-09 I need this pattern:yyyy-MM-dd. But something doesn't work.
我的网络模块 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>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</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.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.4</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.punko.web_app.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
我使用了错误的端口。我需要 8090,而不是 8080。谢谢