如何进行过滤映射
How to do the mapping for filtering
我正在使用 spring 引导,我正在尝试使用作者、标题、描述和出版日期来过滤图书列表,但是我无法以正确的方式进行映射:
enter image description here
我对此很陌生,你能纠正我吗?
这是我控制器中的映射部分:
/*
@GetMapping(value = "/filter", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Book> filterBooks(@RequestParam(name = "author", required = true) String author,
@RequestParam(name = "title", required = true) String title,
@RequestParam(name = "description", required = true) String desc,
@RequestParam(name = "publishedDate", required = true) String publishedDate) {
return bookService.filterBooks(author, title, desc, publishedDate);
}*/
@GetMapping(value = "/filter", produces = MediaType.APPLICATION_JSON_VALUE)
public String filterBooks (Model model) {
public List<Book> filterBooks (@RequestParam(name = "author", required = true) String author,
@RequestParam(name = "title", required = true) String title,
@RequestParam(name = "description", required = true) String desc,
@RequestParam(name = "publishedDate", required = true) String publishedDate){
model.addAttribute("filterBooks", filterBooks);
// return bookService.filterBooks(author, title, desc, publishedDate);
return"filter";
}
}
这是我的 filter.html 文件:https://pastebin.com/87rFZj9Z
这里是图书服务实现文件中的过滤函数:
public List<Book> filterBooks(String author, String title, String description, String publishedDate) {
List<Book> filteredBooks = new ArrayList<>();
if (!CollectionUtils.isEmpty(books)) {
filteredBooks.addAll(books.stream()
.filter(book -> filterByAuthor(book, author) && filterByTitle(book, title)
&& filterByDesc(book, description) && filterByPublishDate(book, publishedDate))
.collect(Collectors.toList()));
}
return filteredBooks;
}
private boolean filterByAuthor(Book book, String author) {
return StringUtils.isEmpty(author) ? Boolean.TRUE : book.getAuthor().equalsIgnoreCase(author);
}
private boolean filterByTitle(Book book, String title) {
return StringUtils.isEmpty(title) ? Boolean.TRUE : book.getTitle().equalsIgnoreCase(title);
}
private boolean filterByDesc(Book book, String description) {
return StringUtils.isEmpty(description) ? Boolean.TRUE : book.getDescription().equalsIgnoreCase(description);
}
private boolean filterByPublishDate(Book book, String publishDate) {
return StringUtils.isEmpty(publishDate) ? Boolean.TRUE : book.getPublish_date().equalsIgnoreCase(publishDate);
}
Hi 示例代码块 RestController Spring boot
@RestController
@RequestMapping("/example")
public class BooksController {
@GetMapping(value = "/filter", produces = MediaType.APPLICATION_JSON_VALUE)
public String filterBooks (@ModelAtribute("model") Model model , @RequestParam(name = "author", required = true) String
author,
@RequestParam(name = "title", required = true) String title,
@RequestParam(name = "description", required = true) String description,
@RequestParam(name = "publishedDate", required = true) String publishedDate){
model.addAttribute("filterBooks", filterBooks);
// return bookService.filterBooks(author, title, description, publishedDate);
return"filter";
}
}
我正在使用 spring 引导,我正在尝试使用作者、标题、描述和出版日期来过滤图书列表,但是我无法以正确的方式进行映射: enter image description here
我对此很陌生,你能纠正我吗? 这是我控制器中的映射部分:
/*
@GetMapping(value = "/filter", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Book> filterBooks(@RequestParam(name = "author", required = true) String author,
@RequestParam(name = "title", required = true) String title,
@RequestParam(name = "description", required = true) String desc,
@RequestParam(name = "publishedDate", required = true) String publishedDate) {
return bookService.filterBooks(author, title, desc, publishedDate);
}*/
@GetMapping(value = "/filter", produces = MediaType.APPLICATION_JSON_VALUE)
public String filterBooks (Model model) {
public List<Book> filterBooks (@RequestParam(name = "author", required = true) String author,
@RequestParam(name = "title", required = true) String title,
@RequestParam(name = "description", required = true) String desc,
@RequestParam(name = "publishedDate", required = true) String publishedDate){
model.addAttribute("filterBooks", filterBooks);
// return bookService.filterBooks(author, title, desc, publishedDate);
return"filter";
}
}
这是我的 filter.html 文件:https://pastebin.com/87rFZj9Z
这里是图书服务实现文件中的过滤函数:
public List<Book> filterBooks(String author, String title, String description, String publishedDate) {
List<Book> filteredBooks = new ArrayList<>();
if (!CollectionUtils.isEmpty(books)) {
filteredBooks.addAll(books.stream()
.filter(book -> filterByAuthor(book, author) && filterByTitle(book, title)
&& filterByDesc(book, description) && filterByPublishDate(book, publishedDate))
.collect(Collectors.toList()));
}
return filteredBooks;
}
private boolean filterByAuthor(Book book, String author) {
return StringUtils.isEmpty(author) ? Boolean.TRUE : book.getAuthor().equalsIgnoreCase(author);
}
private boolean filterByTitle(Book book, String title) {
return StringUtils.isEmpty(title) ? Boolean.TRUE : book.getTitle().equalsIgnoreCase(title);
}
private boolean filterByDesc(Book book, String description) {
return StringUtils.isEmpty(description) ? Boolean.TRUE : book.getDescription().equalsIgnoreCase(description);
}
private boolean filterByPublishDate(Book book, String publishDate) {
return StringUtils.isEmpty(publishDate) ? Boolean.TRUE : book.getPublish_date().equalsIgnoreCase(publishDate);
}
Hi 示例代码块 RestController Spring boot
@RestController
@RequestMapping("/example")
public class BooksController {
@GetMapping(value = "/filter", produces = MediaType.APPLICATION_JSON_VALUE)
public String filterBooks (@ModelAtribute("model") Model model , @RequestParam(name = "author", required = true) String
author,
@RequestParam(name = "title", required = true) String title,
@RequestParam(name = "description", required = true) String description,
@RequestParam(name = "publishedDate", required = true) String publishedDate){
model.addAttribute("filterBooks", filterBooks);
// return bookService.filterBooks(author, title, description, publishedDate);
return"filter";
}
}