如何在弹簧启动休息控制器中使用多个参数更多?
我想获得以下 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);
}
I want to achive the follwing URL to access my data with 2 params:
http://localhost:8080/contactnote?entryDate=2022-02-01?contactType=T
Both mappings with a single param is working:
@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);
But when I try to combine them with two params it wont work, not one of them is usable.
@GetMapping(params ={"entryDate", "contactType"})
public ResponseEntity<Collection<ContactNote>> findByEntryDateGreaterThanAndContactTypeWith(
@RequestParam(value = "entryDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date entryDate,
@RequestParam(value = "contactType") String contactType
)
My reposiitory looks like this:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您输入的 URL 无效,双
?
,您必须使用&
作为第二个参数正确的一个:
http://localhost:8080/contactnote? EntryDate=2022-02-01&contactType=T
此代码有效,使用
name
而不是value
the URL you entered is invalid, double
?
, you have to use&
for the second parametercorrect one:
http://localhost:8080/contactnote?entryDate=2022-02-01&contactType=T
this code work, use
name
insteadvalue