我正在使用 play 框架按照以下文档创建 REST api:https://www.playframework.com/documentation/2.5.x/JavaJsonActions#Serving-a-JSON-response
Controller 代码片段:
public Result sayHello() {
ObjectNode result = Json.newObject();
result.put("exampleField1", "foobar");
result.put("exampleField2", "Hello world!");
return ok(result);
}
路由条目:
GET /response-json controllers.Application.getJSONResponse
我在提供此服务时遇到错误:“找不到符号 符号:ObjectNode 类 位置:类 Controller .Application"
请告诉我哪里出错了?
最佳答案
看来您可能只是在 Controller 中缺少一些导入(文档中的示例未显示所有详细信息)。
所以你的 Controller 可能看起来像这样 - 注意导入:-
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);
}
}
另请注意,您的路线条目必须是:-
GET /response-json controllers.Application.sayHello
关于java - Play Framework : rest api creation issue : cannot find symbol symbol: class ObjectNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40690420/