2

I am reading and learning Spring Boot data with MongoDB. I have about 10 records in my database in the following format:

{
    "_id" : ObjectId("5910c7fed6df5322243c36cd"),
    name: "car"
}

Below are my Java/Spring classes:

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

@Document
public class Item implements Serializable {
    private static final long serialVersionUID = -4343106526681673638L;

    @Id
    private String id;
    private String name;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

@RepositoryRestResource(collectionResourceRel = "item", path = "items")
public interface ItemRepository<T, ID extends Serializable> extends MongoRepository<Item, String>, ItemRepositoryCustom {

}

MongoDB Configuration:

@Configuration
@EnableMongoRepositories
public class MongoConfiguration extends AbstractMongoConfiguration {

    @Override
    protected String getDatabaseName() {
        return "itemdb";
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient("localhost", 27017);
    }

    @Override
    protected String getMappingBasePackage() {
        return "com.learning.domain";
    }
}

I have followed the docs from spring, but when I open the link in my browser:

http://localhost:1337/items

I do not get any of the items inside my database. All I get is:

{
  "_embedded" : {
    "item" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:1337/items"
    },
    "profile" : {
      "href" : "http://localhost:1337/profile/items"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 0,
    "totalPages" : 0,
    "number" : 0
  }
}

The _embedded object is empty. Do I have to explicitly create a controller and implement the methods, or does Spring-Data do it automatically? For example, I want to get the counts and I did this:

http://localhost:1337/items/count

But it gave me a 404.

3
  • How did you add the items to the database? Commented Sep 2, 2017 at 0:42
  • Using Mongobooster IDE. Commented Sep 2, 2017 at 0:45
  • I found the solution. My MongoConfiguration was not being picked. I added the parameters in my properties file and now it works. Commented Sep 2, 2017 at 1:59

1 Answer 1

4

My MongoConfiguration was not being picked, so I added the details in application.properties as:

spring.data.mongodb.database=itemdb

This solved the issue and now I am getting all items from my query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.