ReactiveRepository.save()在Couchbase中不起作用(扩展ReactiveCrudRepository)

时间:2018-03-10 13:41:20

标签: java spring couchbase project-reactor

我无法将文档保存到couchbase。

这是我的代码库。 我在docker中运行了couchbase。

我使用查询在DB中手动添加了一个文档。

repo.findAll()工作正常,但repo.save(person)不起作用。

有人可以帮我吗?

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;
       });
}

这些是我在POM中添加的依赖关系。

    <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>

1 个答案:

答案 0 :(得分:2)

请在此处查看我的回答: updating object with spring data mongodb and kotlin is not working

这是同样的问题:没有任何事情发生,因为没有人订阅此发布者(Mono)。返回Mono(repo.save(person))作为Spring控制器的响应将解决问题。 Spring将订阅Mono,将调用repo.save(person)方法,而Spring将发送HTTP响应。