问题描述
(注意:this 是我工作的基础)
我正在尝试在 Spring Boot 中开发一个简单的 API,但是,我创建的存储库没有正确公开。
模型如下所示:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Genre {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String genreName;
private String genreDesc;
public String getGenreName() {
return genreName;
}
public void setGenreName(String genreName) {
this.genreName = genreName;
}
public String getGenreDesc() {
return genreDesc;
}
public void setGenreDesc(String genreDesc) {
this.genreDesc = genreDesc;
}
}
存储库如下所示:
import java.util.List;
import com.uts13244177.models.Genre;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "genres",path = "genres")
public interface GenreRepository extends PagingAndSortingRepository<Genre,Long>{
List<Genre> findByGenreName(@Param("genreName") String genreName);
}
并且应用程序类与 Spring Initializr 相同:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BookDatabaseApplication {
public static void main(String[] args) {
SpringApplication.run(BookDatabaseApplication.class,args);
}
}
根据我正在使用的上面链接的教程,当我在 http://localhost:8080 上运行 cURL 时,我应该看到以下内容:
{
"_links" : {
"genres" : {
"href" : "http://localhost:8080/genres{?page,size,sort}","templated" : true
}
}
}
然而,我看到的是:
{
"_links" : {
"profile" : {
"href" : "http://localhost:8080/profile"
}
}
}
我正在使用 Maven 4.0.0 运行 OpenJDK 11、Spring Boot 2.5.0。
解决方法
检查所有模型类和控制器类是否都在子包中。 方法 [1]:https://i.stack.imgur.com/i4xnP.png