Search code examples
javaspringspring-bootspring-mvcresttemplate

Problem with search by date web method use GetMapping


I have web-rest Spring application. For the second entity I have 2 fields with LocalDate: arrivalTime, departureTime. I realized CRUD methods. It's okey. But when I tried to search by date(since arrivalTime to departureTime) nothing happen. Just loading in browser.

My entity:

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

My web controller

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

Service-rest:

     @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 part:

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

My POM for web-module:

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

Solution

  • Thank you, I found a mistake. I needed to use 8090 port instead 8080.