【发布时间】:2021-06-07 09:28:57
【问题描述】:
(注: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"
}
}
}
我正在运行 OpenJDK 11、Spring Boot 2.5.0 和 Maven 4.0.0。
【问题讨论】:
标签: java spring spring-boot rest spring-repositories