我如何在 spring boot rest Controller 中使用多个参数?
How can I use more then one param in a 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);
}
你输入的URL无效,双?
,第二个参数必须用&
正确一个: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";
}
我想实现以下 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);
}
你输入的URL无效,双?
,第二个参数必须用&
正确一个: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";
}