Commit 0dfbe993 authored by Sumaiyya Burney's avatar Sumaiyya Burney

Consumes endpoints with web client

parent b7d1807c
package com.nisum.learning.student.client;
import com.nisum.learning.student.model.Student;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class StudentWebClient {
WebClient webClient = WebClient.builder().baseUrl("http://localhost:8080").build();
public Flux<Student> findAll(){
Flux<Student> students = webClient.get()
.uri("/students")
.exchangeToFlux(clientResponse -> clientResponse.bodyToFlux(Student.class));
return students;
}
public Mono<Student> create(Student newStudent) {
Mono<Student> student = webClient.post()
.uri("/students")
.body(Mono.just(newStudent), Student.class)
.retrieve()
.bodyToMono(Student.class);
return student;
}
public Mono<Student> update(Student s) {
Mono<Student> student = webClient.put()
.uri(uriBuilder -> uriBuilder.path("/students/{id}").build(s.getId()))
.body(Mono.just(s), Student.class)
.retrieve()
.bodyToMono(Student.class);
return student;
}
public void delete(Integer id) {
webClient.delete()
.uri(uriBuilder -> uriBuilder.path("/students/{id}").build(id))
.retrieve();
}
}
package com.nisum.learning.student.repository; package com.nisum.learning.student.repository;
import com.nisum.learning.student.model.Student;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface StudentRepository extends ReactiveMongoRepository<Student, Integer> { public interface StudentRepository<Student, Integer> extends ReactiveMongoRepository<Student, Integer> {
} }
...@@ -10,25 +10,25 @@ import reactor.core.publisher.Mono; ...@@ -10,25 +10,25 @@ import reactor.core.publisher.Mono;
@Service @Service
public class StudentService { public class StudentService {
@Autowired @Autowired
StudentRepository studentRepo; StudentRepository<Student, Integer> studentRepository;
public Flux<Student> findAll() { public Flux<Student> findAll() {
return studentRepo.findAll(); return studentRepository.findAll();
} }
public Mono<Student> findById(Integer id){ public Mono<Student> findById(Integer id){
return studentRepo.findById(id); return studentRepository.findById(id);
} }
public void create(Student newStudent){ public void create(Student newStudent){
studentRepo.save(newStudent).subscribe(); studentRepository.save(newStudent).subscribe();
} }
public Mono<Student> update(Student newStudent){ public Mono<Student> update(Student newStudent){
return studentRepo.save(newStudent); return studentRepository.save(newStudent);
} }
public void deleteById(int id){ public void deleteById(int id){
studentRepo.deleteById(id); studentRepository.deleteById(id);
} }
} }
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