odrotbohm #342 - Expanded copyright headers to 2018.
Removed trailing whitespace in touched files.
Latest commit d854c6d Feb 22, 2018
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src #342 - Expanded copyright headers to 2018. Feb 22, 2018
README.adoc Polished Readme of Spring Data REST Multi-store example. Jul 31, 2014
pom.xml #297 - Upgraded to Boot 2.0 and Spring Data Kay. Oct 6, 2017

README.adoc

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

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.

$ curl http://localhost:8080
{ "_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.

$ curl http://localhost:8080/persons
{ "_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:

$ 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
$ curl http://localhost:8080/persons/3
{ "firstName" : "Greg",
  "lastName" : "Turnquist",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons/3"
    }
  }
}

Excellent!

$ curl http://localhost:8080/treasures
{ "_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:

$ 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
$ curl http://localhost:8080/treasures/53cee60a3004309b49465fbe
{ "name" : "MacBook Pro",
  "description" : "Tool of black magic",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/treasures/53cee60a3004309b49465fbe"
    }
  }
}