程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 GetMapping 按日期搜索 Web 方法的问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 GetMapping 按日期搜索 Web 方法的问题?

开发过程中遇到使用 GetMapping 按日期搜索 Web 方法的问题的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 GetMapping 按日期搜索 Web 方法的问题的解决方法建议,希望对你解决使用 GetMapping 按日期搜索 Web 方法的问题有所启发或帮助;

我有 web-rest Spring 应用程序。对于第二个实体,我有两个带有 LocalDate 的字段:到达时间、出发时间。 我实现了 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 服务尝试向返回 HTML 字符串而非居民列表的 MVC 端点发出请求。在 CRUD 中实现 findAllByTime,而不是 REST 请求。

,

您可能有兴趣使用这个库:https://github.com/turkraft/spring-filter

它将让您运行搜索查询,例如:

/search?filter= average(评分)> 4.5 brand.name ('奥迪'、'路虎')(年份> 2018公里 50000)和颜色“白色”事故为空

它支持多种字段类型,例如枚举、日期和集合。

,

谢谢,我发现了一个错误。我需要使用 8090 端口而不是 8080。

大佬总结

以上是大佬教程为你收集整理的使用 GetMapping 按日期搜索 Web 方法的问题全部内容,希望文章能够帮你解决使用 GetMapping 按日期搜索 Web 方法的问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: