6

I've been looking at the couchbase-java-client project and wondering whether it's possible to use it inside of a dropwizard project.

It seems like it'd be a natural fit, because couchbase is basically a JSON database, but the java client doesn't seem to be compatible with Jackson. As far as I can tell, the couchbase client library includes its own internal implementation of a JSON library that's incompatible with all the other java JSON libs out there, which is really weird.

I found a JacksonTransformers class that looked promising at first. But upon closer inspection, the library is using a shaded version of Jackson (with a rewritten package of com.couchbase.client.deps.com.fasterxml.jackson.core).

Anyhow, since dropwizard uses Jackson and Jersey for marshalling JSON documents through the REST API, what's the least-friction way of using the couchbase-java-client library? Is it even possible in this case?

benjismith
  • 16,559
  • 9
  • 57
  • 80

3 Answers3

3

It is definitely possible to use Couchbase with Dropwizard. The client SDK provides JSON manipulation objects for the developer's convenience but it also allows for delegating JSON processing to a library like Jackson or GSON. Take a look at the RawJsonDocument class here. Basically, you can use a Stringified JSON (coming out of any framework) to create one of those objects and the client SDK will understand it as a JSON document for any operation i.e.:

String content = "{\"hello\": \"couchbase\", \"active\": true}";
bucket.upsert(RawJsonDocument.create("rawJsonDoc", content));
Camilo Crespo
  • 615
  • 5
  • 15
0

It should be possible to make this work.

  1. Client requests to dw server for Resource Person.
  2. DW server requests to couchebase, gets a Pojo back representing Person or JSON representing person.
  3. If it's JSON, create a POJO with Jackson annotations in DW and return that to client
  4. If it's a special couchebase pojo, map that to a Jackson pojo and return to to client
CAB
  • 225
  • 1
  • 7
0

A solution based on @CamiloCrespo answer:

public static Document<String> toDocument(String id, Object value,
        ObjectMapper mapper) throws JsonProcessingException {
    return RawJsonDocument.create(id, mapper.writeValueAsString(value));
}

Keep in mind, that you can't use a simply maper, like ObjectMapper mapper = new ObjectMapper(), with Dropwizard.

You can get it from Environment#getObjectMapper() in the Application#run() method, or use Jackson.newObjectMapper() for tests.

An example of using:

ObjectMapper mapper = Jackson.newObjectMapper();
User user = User.createByLoginAndName("login", "name");
bucket.insert(toDocument("123", user, mapper));
Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67