如何在 Spring Boot Rest Controller 中使用多个参数?

旋转

我想获得以下 URL 以使用 2 个参数访问我的数据:

http://localhost:8080/contactnote?entryDate=2022-02-01?contactType=T

具有单个参数的两个映射都有效:

@GetMapping(params ={"contactType"})
    public ResponseEntity<Collection<ContactNote>> findContactNotesWithEntryDateGreaterThan(@RequestParam(value = "contactType")  String contactType) {
        return new ResponseEntity<>(repository.findByContactType(contactType), HttpStatus.OK);
    }
    @GetMapping(params ={"entryDate"})
    public ResponseEntity<Collection<ContactNote>> findContactNotesWithDateTilGreaterThan(@RequestParam(value = "entryDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date entryDate) {
        return new ResponseEntity<>(repository.findByEntryDateGreaterThan(entryDate), HttpStatus.OK);

但是当我尝试将它们与两个参数结合使用时,它不会起作用,其中没有一个是可用的。

@GetMapping(params ={"entryDate", "contactType"})
    public ResponseEntity<Collection<ContactNote>> findByEntryDateGreaterThanAndContactTypeWith(
            @RequestParam(value = "entryDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date entryDate,
            @RequestParam(value = "contactType") String contactType
    )

我的存储库如下所示:

import de.bhm.zvoove.api.domain.ContactNote;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface ContactNoteRepository extends JpaRepository<ContactNote, Long> {
    List<ContactNote> findByContactType(String ContactType);
    List<ContactNote> findByEntryDateGreaterThanAndContactType(Date entryDate , String ContactType);

}
西蒙娜·西莉亚

您输入的网址无效,双精度?,您必须使用&第二个参数

正确一个:http://localhost:8080/contactnote?entryDate=2022-02-01&contactType=T

此代码有效,请name改用value

@GetMapping(path= "/contactnote")
@ResponseBody
public String findByEntryDateGreaterThanAndContactTypeWith(
        @RequestParam(name = "entryDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date entryDate,
        @RequestParam(name = "contactType", required = false) String contactType) {

    return "Hello World";
}

在此处输入图像描述

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Spring Boot Controller中使用Jquery AJAX

如何在 Spring Boot REST 控制器中使用请求和路径参数?

如何在 Spring Boot Application 中使用 Rest Template 来管理多个端点?

Spring Boot Rest Controller如何返回不同的HTTP状态代码?

如何从Spring Boot Rest Controller获取状态描述

如何为特定的Rest Controller禁用Spring Boot Authentication控制

如何使用Power Mock对Spring Boot Rest Controller和异常处理程序进行单元测试

Spring Boot-如何在REST Controller HTTP PUT上允许CORS

如何在requestBody中将MultipartFile传递给Spring Boot Rest Controller

如何在Spring Boot中在Rest Controller方法端点上应用JSONignore注释?

使用Kotlin解决Spring Boot Rest Controller中的单例

如何在Spring Boot的每个REST调用中使用spring安全性?

Spring Boot Rest Controller非常慢的响应

用于Spring Boot Rest Controller的Junit

如何在Android应用中使用Retrofit 2消费Spring-Boot REST API

为什么使用Apache Camel rest DSL而不是spring boot rest controller?

如何在Spring Controller中使用if ...

如何在对Spring Boot Controller的GET请求中接受LocalDateTime参数?

如何在 Spring boot Controller 中接受 GET 参数并返回适当的对象

如何在GET方法中为Spring Boot Controller类传递多个路径变量?

如何在Spring Boot中使用KeyHolder?

如何在Spring Boot中使用CommonsMultipartResolver

如何在Spring Boot中使用@NotNull?

如何在Spring Boot中使用CommandLineRunner?

如何在Spring Boot中使用ORDER BY?

如何使用Spring Boot和Spring Security保护REST API?

URL参数如何映射到Spring Boot Controller上的对象?

如何在Spring Rest Controller中测试异常?

Spring Boot Rest-如何接受多个标题