Commit 171e9d49 authored by Lokesh Singh's avatar Lokesh Singh

animal rest api added

parent 5839fb0e
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
learning exercise repository. learning exercise repository.
## Spring boot ## Spring boot
differnce between reactive stack and servlet stack
![img.png](img.png) ![img.png](img.png)
...@@ -21,7 +21,16 @@ ...@@ -21,7 +21,16 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
</dependency> </dependency>
<!-- No need extra configuration class, Spring Boot will enable reactive support for MongoDB in this project.
ReactiveMongoTemplate and ReactiveMongoRepository will be configured automatically.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
package com.lokesh.webfluxdemo.config;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
@EnableReactiveMongoRepositories(basePackageClasses = {MongoConfig.class})
public class MongoConfig extends AbstractReactiveMongoConfiguration {
@Value("${mongo.uri}")
String mongoUri;
@Bean
public MongoClient mongoClient() {
return MongoClients.create();
}
@Override
protected String getDatabaseName() {
return "animal";
}
}
package com.lokesh.webfluxdemo.controller;
import com.lokesh.webfluxdemo.model.Animal;
import com.lokesh.webfluxdemo.repository.AnimalRepository;
import com.lokesh.webfluxdemo.resource.AnimalResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.time.Duration;
@RestController
@RequestMapping("/api/v1/animal")
public class AnimalController {
@Autowired
private AnimalRepository animalRepository;
@GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<AnimalResource> findAll() {
return animalRepository.findAll()
.map(AnimalResource::new)
.delayElements(Duration.ofSeconds(1L));
}
}
...@@ -12,19 +12,19 @@ import reactor.core.publisher.Mono; ...@@ -12,19 +12,19 @@ import reactor.core.publisher.Mono;
@RestController @RestController
@RequestMapping("/employees") @RequestMapping("/employees")
public class EmployeeController { public class EmployeeController {
private final EmployeeRepository employeeRepository; // private final EmployeeRepository employeeRepository;
//
public EmployeeController(EmployeeRepository employeeRepository) { // public EmployeeController(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository; // this.employeeRepository = employeeRepository;
} // }
//
@GetMapping("/{id}") // @GetMapping("/{id}")
private Mono<Employee> getEmployeeById(@PathVariable String id) { // private Mono<Employee> getEmployeeById(@PathVariable String id) {
return employeeRepository.findEmployeeById(id); // return employeeRepository.findEmployeeById(id);
} // }
//
@GetMapping // @GetMapping
private Flux<Employee> getAllEmployees() { // private Flux<Employee> getAllEmployees() {
return employeeRepository.findAllEmployees(); // return employeeRepository.findAllEmployees();
} // }
} }
package com.lokesh.webfluxdemo; package com.lokesh.webfluxdemo.functionalapi;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
......
package com.lokesh.webfluxdemo; package com.lokesh.webfluxdemo.functionalapi;
import com.lokesh.webfluxdemo.functionalapi.ExampleHandler;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
......
package com.lokesh.webfluxdemo.model;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Builder
@Document
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Animal {
@Id
private String id;
private String name;
private String kingdom;
}
package com.lokesh.webfluxdemo.repository;
import com.lokesh.webfluxdemo.model.Animal;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AnimalRepository extends ReactiveMongoRepository<Animal, String> {
}
package com.lokesh.webfluxdemo.resource;
import com.lokesh.webfluxdemo.model.Animal;
import lombok.Data;
@Data
public class AnimalResource {
private String id;
private String name;
private String kingdom;
public AnimalResource(Animal animal) {
this.id = animal.getId();
this.name = animal.getName();
this.kingdom = animal.getKingdom();
}
}
package com.lokesh.webfluxdemo.sampledata;
import com.lokesh.webfluxdemo.model.Animal;
import com.lokesh.webfluxdemo.repository.AnimalRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
@Component
@Slf4j
public class DataIntializer implements CommandLineRunner {
private final AnimalRepository animalRepository;
public DataIntializer(AnimalRepository animalRepository){
this.animalRepository = animalRepository;
}
@Override
public void run(String[] args) {
log.info("start data initialization...");
this.animalRepository
.deleteAll()
.thenMany(
Flux.just("Cat", "Dog", "Horse", "Cow")
.flatMap(
name -> this.animalRepository.save(Animal.builder().name(name).kingdom("Vertebrate").build())
)
)
.log()
.subscribe(
null,
null,
() -> log.info("done initialization...")
);
}
}
spring:
data:
mongodb:
uri: mongodb://localhost:27017/animal
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment