Issue
I want to write the first program webFlux
and ReactiveMongoRepository
.
i have repository:
@Repository
public interface personRepository extends ReactiveMongoRepository<Person,String> {
Mono<Person> save(Person person);
}
my service:
@AllArgsConstructor
@Service
public class personServiceImpl implements personService{
personRepository repository;
@Override
public Flux<Person> findAll() {
return repository.findAll();
}
@Override
public Mono<Person> saveOrUpdate(Person person) {
CompletableFuture<Person> future = CompletableFuture.supplyAsync(() -> {
repository.save(person);
return person;
});
return Mono.fromFuture(future);
}
}
and the rest service itself:
@RestController
@AllArgsConstructor
public class personController {
personServiceImpl personService;
@GetMapping("/all")
Flux<Person> getAllPerson(){
return personService.findAll();
}
@PostMapping("/save")
public Mono<Person> post(@RequestBody Person user) {
System.out.println("inside***************");
return personService.saveOrUpdate(user);
}
}
Now when I want to test the service and save or find everyone via postman, then I get an error:
"path": "/all",
"status": 405,
"error": "Method Not Allowed",
That is, as I understand it, the request does not even reach the function, but an error is immediately thrown, where can there be an error here?
Solution
The issue seems to be in saveOrUpdate()
method. You don't actually need the CompletableFuture
(why would you in this case?) and the following should work:
@AllArgsConstructor
@Service
public class personServiceImpl implements personService{
personRepository repository;
@Override
public Flux<Person> findAll() {
return repository.findAll();
}
@Override
public Mono<Person> saveOrUpdate(Person person) {
return repository.save(person);
}
}
Answered By - João Dias