无法通过 Spring 引导找到 Spring 数据休息端点

Can't find Spring Data Rest Endpoint with Spring Boot

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

申请class

@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')
}

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

请参阅创建资源控制器 here for more details

好的,修复它我需要做的就是在我的主应用程序上添加一些用于扫描的注释。我不得不告诉它找到实体和存储库,因为它们在不同的包中。

@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);
    }
}

在 spring boot 中,默认情况下您不需要执行 @ComponentScan@EntityScan@EnableJpaRepositories 。将 @RepositoryRestResource 添加到您的存储库。从 starter class 中删除除 @SpringBootApplication 之外的所有注释。 Spring 开机默认会扫描所有包。然后您将能够找到端点 localhost:8080/books。它对您不起作用,因为您专门只扫描实体 class 。未扫描存储库。当您删除这些注释时,将扫描所有包并创建存储库 bean,并且端点将可用。

如果您使用多个包,请确保将您的应用程序启动器保留在基础包中。示例:

src
    main
        java
            com.myapp.springstuff
                Application.java
                package1
                package2