fakhruddin tahery
fakhruddin tahery

Reputation: 89

ReactiveRepository.save() does not work in Couchbase (extending ReactiveCrudRepository)

I'm not able to save the document to couchbase.

Here is my codebase. I'm running couchbase in docker.

I manually added a document in DB using query.

repo.findAll() works fine but repo.save(person) doesn't work.

Can someone please help me in this?

Person.java

@Document
@Getter
@Setter
@AllArgsConstructor
public class Person {

    @Id
    private String id;

    @Field
    @NotNull
    private String firstName;

    @Field
    @NotNull
    private String lastName;

    @Field
    @NotNull
    private String created;

    @Field
    private String updated;
}

PersonRepository.java

public interface PersonRepository extends ReactiveCrudRepository<Person,String> {
    Flux<Person> findByFirstName(String firstName);
    Flux<Person> findByLastName(String lastName);
}

PersonService.java

public interface PersonService {

   @View(designDocument = "_design/person",viewName = "all")
   Flux<Person> findAll();

   Flux<Person> findByFirstName(String firstName);

   Flux<Person> findByLastName(String lastName);

   void create(Person person);
}

PersonRepositoryService.java

@Service
@Slf4j
public class PersonRepositoryService implements PersonService{

    @Autowired
    private PersonRepository repo;

    @Override
    public Flux<Person> findAll() {
       return repo.findAll();
    }

    public Flux<Person> findByFirstName(String firstName){
        return repo.findByFirstName(firstName);
    }

    @Override
    public Flux<Person> findByLastName(String lastName) {
       return repo.findByLastName(lastName);
    }

    public void create(Person person){
       person.setCreated(LocalDateTime.now().toString());
       repo.save(person);
    }
}

ICustomerAccountAPI.java

@RequestMapping("/customerAccountManagement/v1/")
public interface ICustomerAccountAPI {

    @PostMapping(value = "/createProfile", consumes = APPLICATION_JSON)
    @ResponseStatus(HttpStatus.OK)
    Mono<Person> createCustomerProfile(@RequestBody @Valid Mono<Person> person);
}

CustomerAccountAPI.java

@RestController
@Slf4j
public class CustomerAccountAPI implements ICustomerAccountAPI {

   @Autowired PersonRepositoryService personRepositoryService;

   @Override
   public Mono<Person> createCustomerProfile(@RequestBody Mono<Person> person) {

       return person.map( person1 ->{
           personRepositoryService.create(person1);
           return person1;
       });
}

These are the dependencies I have added in POM related to this.

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-couchbase</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.couchbase.client</groupId>
        <artifactId>java-client</artifactId>
        <version>2.5.6</version>
    </dependency>

    <dependency>
        <groupId>io.reactivex</groupId>
        <artifactId>rxjava-reactive-streams</artifactId>
        <version>0.3.0</version>
    </dependency>

Upvotes: 1

Views: 1580

Answers (1)

mroman
mroman

Reputation: 1668

Please see my answer here: updating object with spring data mongodb and kotlin is not working

It is the same problem: nothing happens because noone is subscribed to this Publisher (Mono). Returning the Mono ( repo.save(person) ) as Spring controller’s response will resolve the issue. Spring will subscribe to that Mono, the repo.save(person) method will be called and than Spring will send HTTP response.

Upvotes: 2

Related Questions