How to use Couchbase Java Client in Dropwizard project?

I watched the couchbase-java-client project and wondered if it could be used inside the dropwizard .

It seems like this would be a natural approach, because couchbase is basically a JSON database, but the java client does not seem to be compatible with Jackson. As far as I can tell, the couchbase client library includes its own internal implementation of the JSON library , which is incompatible with all other JSON java libraries, which is really strange.

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

Anyway, since dropwizard uses Jackson and Jersey to sort JSON documents via the REST API, what is the least friction way to use the couchbase-java-client library? Is this possible in this case?

+6
source share
3 answers

You can use Couchbase with Dropwizard. The client SDK provides JSON manipulation objects for the convenience of developers, but also allows you to delegate JSON processing to a library such as Jackson or GSON. Take a look at the RawJsonDocument class here . Basically, you can use Stringified JSON (coming from any environment) to create one of these objects, and the client SDK will understand it as a JSON document for any ie operation:

 String content = "{\"hello\": \"couchbase\", \"active\": true}"; bucket.upsert(RawJsonDocument.create("rawJsonDoc", content)); 
+3
source

This work needs to be done.

  • The client requests a dw server for the resource person.
  • The DW server requests couchebase, gets back a Pojo representing Person or JSON representing a person.
  • If it is JSON, create a POJO with Jackson annotations in DW and return it to the client
  • If it's a special couchebase pojo, match it to Jackson pojo and get back to the client
0
source

The solution based on @CamiloCrespo answers:

 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 cannot use a simple pointer 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.

Usage example:

 ObjectMapper mapper = Jackson.newObjectMapper(); User user = User.createByLoginAndName("login", "name"); bucket.insert(toDocument("123", user, mapper)); 
0
source

Source: https://habr.com/ru/post/977721/


All Articles