无法在Spring Boot中找到Spring Data Rest端点

3

我第一次尝试使用Spring Data Rest,但是我似乎找不到我的存储库端点在localhost:8080/books。有人看到我配置错了什么吗?

应用程序类

@SpringBootApplication
@ComponentScan(basePackageClasses = {Book.class})
public class SpringDataMicroServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataMicroServiceApplication.class, args);
    }
}

书籍实体

@lombok.Getter
@lombok.Setter
@lombok.RequiredArgsConstructor
@lombok.EqualsAndHashCode(of = "isbn")
@lombok.ToString(exclude="id")
@Entity
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    private String isbn;

    private String title;

    private String author;

    private String description;
}

书籍仓库

public interface BookRepository extends CrudRepository<Book, Long> {
}

Gradle构建文件

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE'
    }
}

apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'idea'

dependencyManagement {
    imports {
        mavenBom 'io.spring.platform:platform-bom:2.0.5.RELEASE'
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.data:spring-data-rest-hal-browser')
    compile('org.projectlombok:lombok:1.16.6')
    compile('org.springframework.retry:spring-retry')
    compile('org.springframework.boot:spring-boot-starter-validation')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-ws')
    compile('org.springframework.boot:spring-boot-actuator')

    runtime('com.h2database:h2')

    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}
3个回答

7
在Spring Boot中,默认情况下无需执行@ComponentScan、@EntityScan和@EnableJpaRepositories。只需要在您的repository中添加@RepositoryRestResource注解即可。从starter类中移除除@SpringBootAplication之外的所有注解,Spring Boot将默认扫描所有包。然后,您就可以找到端点localhost:8080/books。如果没有按照这种方式进行操作,可能会因为您正在特别扫描实体类而无法使用。存储库未被扫描。当您删除这些注解时,所有包都将被扫描,存储库bean也将被创建,端点将可用。
如果您使用多个包,请确保将应用程序启动器保留在基础包中。例如:
src
    main
        java
            com.myapp.springstuff
                Application.java
                package1
                package2

有趣,我一定会尝试的。如果Spring Boot可以找到所有内容,减少注释会很好。迫不及待想要测试这个。 - Matthew Fontana
你试过了吗? - Fahad Fazil
经过一些测试,看起来我肯定需要注释。目前我有一个分散了应用程序和领域的包。如果没有这3个注释,我不会加载任何有关书籍的端点。看起来我可以删除组件扫描,但是如果我删除存储库或JpaRepositories中的任何一个,我都会遇到问题。 - Matthew Fontana
奇怪。多个包? - Matthew Fontana
是的,多个包。您能共享项目吗? - Fahad Fazil
显示剩余3条评论

4

好的,我已经解决了所有问题。我只需要在我的主应用程序中添加一些扫描注释即可。因为实体和仓库位于不同的包中,所以我必须告诉它去寻找它们。

@SpringBootApplication
@ComponentScan(basePackageClasses = {BookRepository.class})
@EntityScan(basePackageClasses = {Book.class})
@EnableJpaRepositories(basePackageClasses = {BookRepository.class})
public class SpringDataMicroServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataMicroServiceApplication.class, args);
    }
}

-1

你缺少一个资源控制器 (@RestController),它可以将 GET 请求路由到 /books (@RequestMapping("/books")) 的 Java 方法,以便从数据库中检索 Book 实体。

请参见 此处 了解更多详细信息。


我正在使用Spring Data Rest,它将基于我的实体模型生成REST控制器。然而,看起来我在某个地方配置错误。 - Matthew Fontana
你说得对,@MatthewFontana,在这个例子中确实没有控制器:https://github.com/spring-projects/spring-data-book - ck1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接