使用getmapping按日期搜索web方法的问题

col17t5w  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(267)

我有一个webrestspring应用程序。对于第二个实体,我有两个localdate字段:arrivaltime、departuretime。我意识到了积垢的方法。没关系。但当我试图按日期(从到达时间到离开时间)搜索时,什么也没发生。正在浏览器中加载。
我的实体:

@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;
}

我的web控制器

@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 for web模块:

<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>
ni65a41a

ni65a41a1#

rest服务尝试向mvc端点请求返回html字符串,而不是驻留列表。在crud中实现findallbytime,而不是rest请求。

相关问题