使用Spring-data-couchbase

时间:2019-04-25 09:39:16

标签: spring-boot spring-data-couchbase

我需要阅读一个沙发文档,并将其转换为简单的pojo类对象。 从docs中,我了解到我需要定义一些自定义转换器,以将该文档转换为POJO对象,因为它具有类型为org.apache.commons.json.JSONObject的(非原始)字段,如下所示

@Document
public class MyClass{

    @Id
    private String id;

    @Field
    private JSONObject someJsonObj;
//getters and setters

}

以下是此存储库

@Repository
@N1qlPrimaryIndexed
public interface MyClassRepository extends CouchbaseRepository<MyClass, String>{}

在保存对象时,下面的代码可以正常工作,

     MyClass myclass = new MyClass();
     myclass.setId(someKey);
    myclass.setSomeJsonObj(jsonObject);
    myclassRepository.save(myclass);

并以下面的格式将文档保存在沙发上

someKey {jsonObject}

现在在检索过程中,我正在使用如下所示的自定义读取转换器

@ReadingConverter
    public static enum CouchbaseDocumentToMyClassConverter implements Converter<CouchbaseDocument,MyClass> {
        INSTANCE;
        @Override
        public MyClass convert(CouchbaseDocument source) {
            System.out.println(source.getPayload()); //this is returning lot of unwanted data like id, exp and payload for every nested json Object 
            //logic to instantiate MyClass obj 
            return new MyClass();
            }
        }

如果我尝试将对象直接映射到MyClass对象而不是转换中的CouchbaseDocument,那么我将得到org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type java.lang.Object!。以下是完整的stacktrace

2019-04-25 15:01:36.584 ERROR [{}] 33024 --- [http-nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type java.lang.Object!] with root cause
rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: com.couchbase.client.java.document.RawJsonDocument.class
    at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.Subscriber.setProducer(Subscriber.java:211) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) ~[rxjava-1.3.8.jar:1.3.8]
    at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) ~[rxjava-1.3.8.jar:1.3.8]

有人可以提供一些有关如何解决此问题的见解吗?我直接想在检索过程中将沙发床文档映射到我的POJO类对象。

1 个答案:

答案 0 :(得分:1)

如果您使用的是Spring Data,则无需编写任何自定义转换器,存储库本身可以返回POJO:

@Document
@Data
@AllArgsConstructor
public class Area extends BasicEntity {

    @NotNull
    @Id
    private String id;

    @NotNull
    private String name;

    private String description;

    private String parentAreaId;

}

然后,在您的存储库中:

@N1qlPrimaryIndexed
public interface AreaRepository extends CouchbaseRepository<Area, String> {

    List<Area> findByParentAreaId(String parentAreaId);
}

仅当要存储的对象不是POJO(如JODA或与Java 8 Date相关的类)时,才能定义自定义转换器。