【问题标题】:Spring Boot isn't exposing proper repository routesSpring Boot 没有公开正确的存储库路由
【发布时间】: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


    【解决方案1】:

    检查所有模型类和控制器类是否在一个子包中。 方法 [1]:https://i.stack.imgur.com/i4xnP.png

    【讨论】:

    • 谢谢你,原来问题是我错误地将我的模型和存储库放在一个目录太高,所以主应用程序类没有读取它们。
    猜你喜欢
    • 2017-08-23
    • 1970-01-01
    • 2023-03-28
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2016-09-30
    • 2021-05-27
    相关资源
    最近更新 更多