我正在做网上书店项目。我试图通过使用query作为参数通过postman发送get请求来找到一本书:http://localhost:8080/api/v1/books/search?查询=托尔斯泰。403错误提示当我调试时,我意识到当我发送get请求时,我的控制器没有被调用。这是我的控制器:
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/books")
public class BookController {
private final BookService bookService;
@GetMapping("/search")
public ResponseEntity<Set<Books>> searchBook(@RequestParam(value = "query",defaultValue = "a") String query){
return ResponseEntity.ok(bookService.searchBooks(query));
}
}
字符串
下面是我的服务为这个控制器:
@Service
@RequiredArgsConstructor
@Log4j2
public class BookServiceImpl implements BookService{
private final BooksRepository booksRepository;
@Override
public Optional<Books> getById(Long id) {
return booksRepository.findById(id);
}
@Override
public Set<Books> searchBooks(String query) {
List<Books> booksTitle = booksRepository.searchBookByTitle(query);
List<Books> booksGenre = booksRepository.searchBookByGenre(query);
List<Books> booksAuthor = booksRepository.searchByAuthor(query);
Set<Books> books = Stream.of(booksTitle,booksGenre,booksAuthor).flatMap(Collection::stream).collect(Collectors.toSet());
return books;
}
型
这里是repository:
@Repository
public interface BooksRepository extends JpaRepository<Books,Long> {
Optional<Books> findById(Long id);
@Query(value = "SELECT b FROM books b WHERE b.title LIKE (%:query%)",nativeQuery = true)
List<Books> searchBookByTitle(String query);
@Query(value = "SELECT b FROM books b WHERE b.genre LIKE (%:query%)",nativeQuery = true)
List<Books> searchBookByGenre(String query);
@Query(value = "SELECT b FROM books b inner join books_and_authors bas on b.id = bas.book_id " +
"inner join authors a on bas.author_id = a.id WHERE a.name LIKE (%:query%)",nativeQuery = true)
List<Books> searchByAuthor(String query);
}
型
感激任何我能得到的帮助;
2条答案
按热度按时间j2cgzkjk1#
您正在使用SpringSecurity吗?乍一看,你的电话没有什么错。
如果您使用的是spring Security,请在config类中验证您的权限。
xzabzqsa2#
不确定这是否可以阻止您的控制器接收请求,但您的
BookServiceImpl
类在private final BooksRepository booksRepository;
之前缺少@Autowired
注解尝试替换
字符串
与
型