0

I looked at the MongoDB REST Getting started guide from Spring (https://spring.io/guides/gs/accessing-mongodb-data-rest/). When I'm adding an entity in a different package then the Application.java, say

com.project.rest.core.entities.Account.java

and do the same for the repository

com.project.rest.core.repositories.AccountRepo.java

the Application does not recognize the REST endpoints under localhost:8080 after building with. It just shows

{
    "_links": {
        "people": {
            "href": "http://localhost:8080/people{?page,size,sort}",
            "templated": true
        },
        "profile": {
            "href": "http://localhost:8080/profile"
        }
    }
}

When I put the Account.java and AccountRepo.java in the same package where the Application.java resides, it works.

So, how do I integrate Repositories from different packages in the Application?

Best regards,

Tim

EDIT: My main application class looks as follows:

@SpringBootApplication
public class Application {

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

Even with added @ComponentScan(basePackages="com.project.rest") the AccountRepository is not found by Spring boot.

The repository has the following annotation:

@RepositoryRestResource(collectionResourceRel = "accounts", path="accounts")

2 Answers 2

1

You need to have the Components and Repositories under any of the sub-packages as Application.java.

From Spring Docs

We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.

1
  • Yes, I tried exactly that and now it works. Thank you very much Kapil!
    – user1337
    Commented Mar 14, 2016 at 5:46
0

Spring Boot should handle it with auto configuration annotation SpringBootApplicationor EnableAutoConfiguration.

Also you can specify annotation ComponentScan scanning by adding additional annotation to the main application class.

@ComponentScan(basePackages="com.project")
1
  • Thanks for your answer Anton. Even with added @ComponentScan(basePackages="com.project.rest") the AccountRepo is not found. I edited my question for additional information.
    – user1337
    Commented Mar 14, 2016 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.