Can we create custom Query using Spring Data REST while using @RepositoryRestResource?

匿名 (未验证) 提交于 2019-12-03 01:41:02

问题:

I am new to spring boot, I was trying to create a simple REST service using Spring-boot JPA. First I used simple @Repository which works fine, I can write custom Query method for the properties defined in my class. But when I use @RepositoryRestResource I am not able to create custom Query methods. Basic finding on primary key and other operations are happening but I can not create any custom Query.

package com.example.demo.Model;  import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;  @Entity public class Book {      @Id     @GeneratedValue     int id;     int isbn;     String name;     String author;     double price;     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public int getIsbn() {         return isbn;     }     public void setIsbn(int isbn) {         this.isbn = isbn;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public String getAuthor() {         return author;     }     public void setAuthor(String author) {         this.author = author;     }     public double getPrice() {         return price;     }     public void setPrice(double price) {         this.price = price;     }  } 

This is Repository

package com.example.demo.repo;  import java.util.List;  import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Repository;  import com.example.demo.Model.Book;  @RepositoryRestResource(collectionResourceRel = "books", path = "books") public interface BookRepo extends JpaRepository<Book, Integer> {      //I want this query to work same as it does for primary Key id.     public Book findByName(@Param("name") String name);      //Same for this     public Book findByIsbn(@Param("isbn") String isbn);      } 

Since I am not mapping any url for searching by name, I am trying to search like this localhost:8080/books/?name=java Is it correct? For above url it simply behaves like localhost:8080/books and ignore subsequent part and provides all books details. I just want to have two classes and perform all basic rest operation and create custom Query.

Thank you in advance.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!