Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reactive mongodb sample #9761

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions spring-boot-samples/README.adoc
Expand Up @@ -56,6 +56,9 @@ The following sample applications are provided:
| link:spring-boot-sample-data-ldap[spring-boot-sample-data-ldap]
| Stores data using Spring Data LDAP

| link:spring-boot-sample-data-mongodb-reactive[spring-boot-sample-data-mongodb-reactive]
| Stores data using Spring Data MongoDB Reactive

| link:spring-boot-sample-data-mongodb[spring-boot-sample-data-mongodb]
| Stores data using Spring Data MongoDB

Expand Down
1 change: 1 addition & 0 deletions spring-boot-samples/pom.xml
Expand Up @@ -39,6 +39,7 @@
<module>spring-boot-sample-data-jpa</module>
<module>spring-boot-sample-data-ldap</module>
<module>spring-boot-sample-data-mongodb</module>
<module>spring-boot-sample-data-mongodb-reactive</module>
<module>spring-boot-sample-data-neo4j</module>
<module>spring-boot-sample-data-redis</module>
<module>spring-boot-sample-data-rest</module>
Expand Down
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-data-mongodb-reactive</artifactId>
<name>Spring Boot Data MongoDB Sample</name>
<description>Spring Boot Data MongoDB Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,42 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sample.data.mongoreactive;

import org.springframework.data.annotation.Id;

public class Customer {

@Id
private String id;

private String firstName;
private String lastName;

public Customer() {
}

public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer[id=%s, firstName='%s', lastName='%s']", id,
firstName, lastName);
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sample.data.mongoreactive;

import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface ReactiveCustomerRepository
extends ReactiveMongoRepository<Customer, String> {

Mono<Customer> findByFirstName(Mono<String> firstName);

Flux<Customer> findByLastName(Mono<String> lastName);
}
@@ -0,0 +1,65 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sample.data.mongoreactive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@SpringBootApplication
public class SampleMongoReactiveApplication implements CommandLineRunner {

@Autowired
private ReactiveCustomerRepository repository;

public void run(String... args) throws Exception {
System.out.println("Deleting all Customers");
this.repository.deleteAll().then().subscribe();

// save a couple of customers
this.repository.saveAll(
Flux.just(new Customer("Alice", "Smith"), new Customer("Bob", "Smith")))
.then().subscribe();

// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");

repository.findAll().subscribe(System.out::println);

System.out.println();

// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
this.repository.findByFirstName(Mono.just("Alice")).log()
.subscribe(System.out::println);

System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
this.repository.findByLastName(Mono.just("Smith")).log()
.subscribe(System.out::println);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(SampleMongoReactiveApplication.class, args);
}
}
@@ -0,0 +1,30 @@
package sample.data.mongoreactive;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.test.context.junit4.SpringRunner;

/**
* Tests for {@link SampleMongoReactiveApplicationTest}.
*
* @author Raja Kolli
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleMongoReactiveApplicationTest {

@ClassRule
public static OutputCapture outputCapture = new OutputCapture();

@Test
public void testDefaultSettings() throws Exception {
String output = SampleMongoReactiveApplicationTest.outputCapture.toString();
assertThat(output).contains("firstName='Alice', lastName='Smith'");
}

}