Matthew Fontana Matthew Fontana - 5 months ago 14
Java Question

Can't find Spring Data Rest Endpoint with Spring Boot

I am trying out Spring Data Rest for the first time, but I can't seem to find my repository endpoint at

localhost:8080/books
Does anyone see what I configured wrong?

Application class

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

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


Book Entity

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


BookRepository

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


Gradle Build file

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

Answer

In spring boot , by default you don't need to do @ComponentScan , @EntityScan and @EnableJpaRepositories . Add @RepositoryRestResource to your repository . Remove all annotations from the starter class except @SpringBootApplication . Spring boot will scan all packages by default . You will be then able to find the endpoint localhost:8080/books . It didn't work for you because you were specifically scanning only the entity class . The repository was not scanned . When you remove those annotations , all packages will be scanned and repository beans will we crated and the endpoints will be available.

If you are using multiple packages make sure to keep your application launcher in the base package. Example:

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