Neo Neo - 20 days ago 5
Java Question

play framework : rest api creation issue : cannot find symbol symbol: class ObjectNode

I'm using play framework to create REST api following this documentation: https://www.playframework.com/documentation/2.5.x/JavaJsonActions#Serving-a-JSON-response

Controller Code snippet:



public Result sayHello() {
ObjectNode result = Json.newObject();
result.put("exampleField1", "foobar");
result.put("exampleField2", "Hello world!");
return ok(result);
}


Routes entry:



GET /response-json controllers.Application.getJSONResponse


Error which I am getting while serving this: "cannot find symbol
symbol: class ObjectNode
location: class controllers.Application
"

Let me know where i'm going wrong ?

Nio Nio
Answer

It seems like you might just be missing a few imports in your Controller (the examples in the docs do not show all the details).

So your Controller might look something like this - note the imports:-

package controllers;

import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import play.mvc.*;

public class Application extends Controller {

    public Result sayHello() {
        ObjectNode result = Json.newObject();
        result.put("exampleField1", "foobar");
        result.put("exampleField2", "Hello world!");
        return ok(result);
    }
}

Also, note that your routes entry needs to be:-

GET     /response-json          controllers.Application.sayHello