Kazuya Tomita Kazuya Tomita - 1 month ago 10
Java Question

com.fasterxml.jackson.databind.node.ObjectNode cannot be converted to org.codehaus.jackson.node.ObjectNode

I'm using play2.2.1 and now I am facing the error:

com.fasterxml.jackson.databind.node.ObjectNode cannot be converted to org.codehaus.jackson.node.ObjectNode

The code is like this:

package controllers;

import play.*;
import play.data.*;
import play.mvc.*;
import play.db.ebean.*;

import views.html.*;

import java.util.*;
import models.*;
import com.avaje.ebean.ExpressionList;

import play.data.validation.Constraints.Required;

import scala.*;

import play.libs.Json;
import org.codehaus.jackson.node.ObjectNode;
public class Application extends Controller {

//create JSON data
public static Result ajax() {
String input = request().body().asFormUrlEncoded().get("input")[0];
ObjectNode result = Json.newObject();
if(input == null) {
result.put("status", "BAD");
result.put("message", "Can't get sending data...");
return badRequest(result);
} else {
result.put("status", "OK");
result.put("message", input);
return ok(result);
}
}


And the error occurs at
ObjectNode result = Json.newObject();
.
However, I confirmed that the return value of the static method
newObject()
is
org.codehaus.jackson.node.ObjectNode
by [][1]https://playframework.com/documentation/2.0/api/java/play/libs/Json.html

The version of the API reference is 2.0, not 2.2, so something different exists here?
Profoundly, when I import
com.fasterxml.jackson.databind.node.ObjectNode
and delete
import org.codehaus.jackson.node.ObjectNode;
, the application works well.
Can anyone explain this behavior?
Why does the error message contradict with the API reference?

Answer

I assume that there are two versions of the Jackson library mixed up in one project. Old versions use the org.codehaus.jackson namespace, while newer versions use com.fasterxml.jackson, because they moved from Codehaus. I had the same problem, when I used a newer version of Jackson and the refactoring did not work properly. The classes are not compatible, so you have to use com.fasterxml.jackson consistently.