= Spring Data REST - Multi-store example

This example app shows how to mix together several Spring Data projects.

* Each data store's domain objects must be split up into distinct packages. That way, the Spring Data store can be configured.
* It's most important to make sure the underlying Spring Data layers work before adding Spring Data REST. Spring Data REST simply delegates and doesn't write extra functionality.
* With both set up properly, things should flow nicely.

== Top level app

[source,java]
----
package example;

// …import statements …

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableMongoRepositories
public class Application {

  private static final Logger log = LoggerFactory.getLogger(Application.class);

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

  @Autowired PersonRepository personRepository;
  @Autowired TreasureRepository treasureRepository;

  @PostConstruct
  void checkitOut() {

    personRepository.save(new Person("Frodo", "Baggins"));
    personRepository.save(new Person("Bilbo", "Baggins"));

    for (Person person : personRepository.findAll()) {
      log.info("Hello " + person.toString());
    }

    treasureRepository.deleteAll();
    treasureRepository.save(new Treasure("Sting", "Made by the Elves"));
    treasureRepository.save(new Treasure("Sauron's ring", "One ring to rule them all"));

    for (Treasure treasure : treasureRepository.findAll()) {
      log.info("Found treasure " + treasure.toString());
    }
  }
}
----

* Notice how `@EnableJpaRepositories(basePackageClasses = Person.class)` uses `Person` as a tip off of what package to start scanning for JPA entities
* Notice how `@EnableMongoRepositories(basePackageClasses = Treasure.class)` uses `Treasure` to tip off where to look for MongoDB documents
* The repository interfaces need to be in the separate packages as well. Otherwise, it doesn't work for the same reason.

To prove it works, two people are created, stored, then retrieved. Then, two treasures are created, stored, and retrieved.

NOTE: The treasures are first cleared out. Otherwise, every time you run this app, it will keep adding.

After launching the app by running `mvn spring-boot:run`, from another shell, you can explore the RESTful interface.

[source, bash]
----
$ curl http://localhost:8080
----
[source, javascript]
----
{ "_links" : {
    "persons" : {
      "href" : "http://localhost:8080/persons"
    },
    "treasures" : {
      "href" : "http://localhost:8080/treasures"
    }
  }
}
----

Here you can see the two resources backed by the repositories of the application, served up seamlessly. Following the `persons` link, you can see the two people created at startup.

[source, bash]
----
$ curl http://localhost:8080/persons
----
[source, javascript]
----
{ "_embedded" : {
    "persons" : [ {
      "firstName" : "Frodo",
      "lastName" : "Baggins",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/1"
        }
      }
    }, {
      "firstName" : "Bilbo",
      "lastName" : "Baggins",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/2"
        }
      }
    } ]
  }
}
----

You can easily create a new entry:

[source, bash]
----
$ curl -X POST -i -H "Content-Type:application/json" -d '{"firstName":"Greg", "lastName":"Turnquist"}' http://localhost:8080/persons

HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/persons/3
Content-Length: 0
Date: Tue, 22 Jul 2014 22:26:17 GMT
----
[source, bash]
----
$ curl http://localhost:8080/persons/3
----
[source, javascript]
----
{ "firstName" : "Greg",
  "lastName" : "Turnquist",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons/3"
    }
  }
}
----

Excellent!

[source, bash]
----
$ curl http://localhost:8080/treasures
----
[source, javascript]
----
{ "_embedded" : {
    "treasures" : [ {
      "name" : "Sting",
      "description" : "Made by the Elves",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/treasures/53cedae13004309b49465fbc"
        }
      }
    }, {
      "name" : "Sauron's ring",
      "description" : "One ring to rule them all",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/treasures/53cedae13004309b49465fbd"
        }
      }
    } ]
  }
}
----

If you venture into `treasures`, you can see the two treasures found in Middle Earth. Let's add another:

[source, bash]
----
$ curl -X POST -i -H "content-type:application/json" -d '{"name":"MacBook Pro", "description":"Tool of black magic"}' localhost:8080/treasures

HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/treasures/53cee60a3004309b49465fbe
Content-Length: 0
Date: Tue, 22 Jul 2014 22:30:34 GMT
----
[source, bash]
----
$ curl http://localhost:8080/treasures/53cee60a3004309b49465fbe
----
[source, javascript]
----
{ "name" : "MacBook Pro",
  "description" : "Tool of black magic",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/treasures/53cee60a3004309b49465fbe"
    }
  }
}
----