helun/Ektorp

POJO exemple fasterxml

elbedd opened this issue · 4 comments

I try to test ektorp with a little project such as : http://ektorp.org/reference_documentation.html#d100e102

But, I want to use POJO object by using com.fasterxml.jackson instead of org.codehaus.jackson
I update from
Ektorp : 1.2.1 / Jackson : 2.0.0
to a later verion :
Ektorp : 1.4.1 / Jackson : 2.2.3

I use CouchDB 1.6.1

The problem is that I'm not able to save my POJO object without an id, and I was not able to do a update (rev is needed !).
{
"error" : "bad_request",
"reason" : "Invalid rev format"
}

It works fine before ( with org.codehaus.jackson).

What must I changed in this simple exemple ?

    {
        package cncouchdb;
        import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
        import com.fasterxml.jackson.annotation.JsonProperty;

        @JsonIgnoreProperties({"id", "revision"})
        public class Sofa {
            @JsonProperty("_id")
            private String id;

        @JsonProperty("_rev")
            private String revision;

            private String color;

            public void setId(String s) {
                id = s;
        }

        public String getId() {
            return id;
        }

        public String getRevision() {
            return revision;
        }

        public void setColor(String s) {
            color = s;
        }

        public String getColor() {
            return color;
        }
    }

My code of the main method for create is:


HttpClient httpClient = new StdHttpClient.Builder()
                .url(DATABASE_URL)
                .build();

        CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);

        CouchDbConnector db = new StdCouchDbConnector(DATABASE_ID, dbInstance);

        db.createDatabaseIfNotExists();

        Sofa sofa = new Sofa();
        sofa.setColor("red");
        sofa.setId(SOFA_ID_TEST);
        db.create(sofa);

For Update

HttpClient httpClient = new StdHttpClient.Builder()
                .url(MainCreate.DATABASE_URL)
                .build();

        CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);

        CouchDbConnector db = new StdCouchDbConnector(MainCreate.DATABASE_ID, dbInstance);

        db.createDatabaseIfNotExists();

        Sofa sofa = db.find(Sofa.class, MainCreate.SOFA_ID_TEST);
        sofa.setColor("blue");
        db.update(sofa);

My Maven pom :

<properties>
    <jackson.version>2.2.3</jackson.version>
  </properties>

  <dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>


    <dependency>
        <groupId>org.ektorp</groupId>
        <artifactId>org.ektorp</artifactId>
        <version>1.4.2</version>
    </dependency>


  </dependencies>

The problem might be that Ektorp fails to set the id and/or revision of
your object.
Write a test that asserts that the class org.ektorp.util.Documents is able
to read/write id and rev on your class.

Here's a little more about the Documents class:
http://ektorp.org/reference_documentation.html#d100e495

//H

On Thu, Feb 26, 2015 at 9:52 PM, BRAUD Laurent notifications@github.com
wrote:

I try to test ektorp with a little project such as :
http://ektorp.org/reference_documentation.html#d100e102

But, I want to use POJO object by using com.fasterxml.jackson instead of
org.codehaus.jackson
I update from
Ektorp : 1.2.1 / Jackson : 2.0.0
to a later verion :
Ektorp : 1.4.1 / Jackson : 2.2.3

I use CouchDB 1.6.1

The problem is that I'm not able to save my POJO object without an id, and
I was not able to do a update (rev is needed !).
{
"error" : "bad_request",
"reason" : "Invalid rev format"
}

It works fine before ( com.fasterxml.jackson instead of
org.codehaus.jackson).

What must I changed in this simple exemple ?

{
    package cncouchdb;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonProperty;

    @JsonIgnoreProperties({"id", "revision"})
    public class Sofa {
        @JsonProperty("_id")
        private String id;

    @JsonProperty("_rev")
        private String revision;

        private String color;

        public void setId(String s) {
            id = s;
    }

    public String getId() {
        return id;
    }

    public String getRevision() {
        return revision;
    }

    public void setColor(String s) {
        color = s;
    }

    public String getColor() {
        return color;
    }
}

My code of the main method for create is:

HttpClient httpClient = new StdHttpClient.Builder()
.url(DATABASE_URL)
.build();

    CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);

    CouchDbConnector db = new StdCouchDbConnector(DATABASE_ID, dbInstance);

    db.createDatabaseIfNotExists();

    Sofa sofa = new Sofa();
    sofa.setColor("red");
    sofa.setId(SOFA_ID_TEST);
    db.create(sofa);

For Update

HttpClient httpClient = new StdHttpClient.Builder()
.url(MainCreate.DATABASE_URL)
.build();

    CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);

    CouchDbConnector db = new StdCouchDbConnector(MainCreate.DATABASE_ID, dbInstance);

    db.createDatabaseIfNotExists();

    Sofa sofa = db.find(Sofa.class, MainCreate.SOFA_ID_TEST);
    sofa.setColor("blue");
    db.update(sofa);

My Maven pom :

2.2.3 com.fasterxml.jackson.core jackson-core ${jackson.version}
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>


<dependency>
    <groupId>org.ektorp</groupId>
    <artifactId>org.ektorp</artifactId>
    <version>1.4.2</version>
</dependency>


Reply to this email directly or view it on GitHub
#230.

By debug with jar source, I found that previously, the #serializeToJson method in org.ektorp.impl.StdCouchDbConnector#create doesn't return null value, but the new do.

I find this change in Jackson : http://wiki.fasterxml.com/JacksonAnnotationSerializeNulls
In my 1st exemple, it works by adding : @JsonSerialize, but it 's deprecated.

In Ektorp, It looks like to have been changed correctly in class org.ektorp.impl.StdObjectMapperFactory

I started getting this error on the upgrade from 1.3.0 to 1.4.x. I tracked it down to the code in #233

@sghill your solution worked for me 👍