从Java控制器返回值

时间:2018-11-17 13:32:57

标签: javascript java angular playframework controller

我是Java控制器的新手。我正在使用Play框架,正在尝试从控制器返回ArrayList,但出现错误。 正确的做法是什么?我的代码在下面。

另一个问题,我想用html和javascript角功能编写最简单的页面,这些功能调用Play框架控制器。除了前端,还有像Play框架这样的简单平台吗?

此方法:

public Result getComments(int postId) {        
    ArrayList<Comment> comments = this.commentsDic.getComments(postId);        
    return ok(comments);
}

出现以下错误:

no suitable method found for ok(java.util.ArrayList<models.Comment>)
method play.mvc.Results.ok(play.twirl.api.Content) is not applicable
  (argument mismatch; java.util.ArrayList<models.Comment> cannot be converted to play.twirl.api.Content)
method play.mvc.Results.ok(java.lang.String) is not applicable
  (argument mismatch; java.util.ArrayList<models.Comment> cannot be converted to java.lang.String)
method play.mvc.Results.ok(com.fasterxml.jackson.databind.JsonNode) is not applicable
  (argument mismatch; java.util.ArrayList<models.Comment> cannot be converted to com.fasterxml.jackson.databind.JsonNode)
method play.mvc.Results.ok(byte[]) is not applicable
  (argument mismatch; java.util.ArrayList<models.Comment> cannot be converted to byte[])
method play.mvc.Results.ok(java.io.InputStream) is not applicable
  (argument mismatch; java.util.ArrayList<models.Comment> cannot be converted to java.io.InputStream)
method play.mvc.Results.ok(java.io.File) is not applicable
  (argument mismatch; java.util.ArrayList<models.Comment> cannot be converted to java.io.File)

以这种方式:

    public ArrayList<Comment> getComments(int postId) {        
    ArrayList<Comment> comments = this.commentsDic.getComments(postId);        
    return comments;
}

给出此错误:

Cannot use a method returning java.util.ArrayList[models.Comment] as a Handler for requests

指向标记行上的路由文件:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# An example controller showing a sample home page
GET     /                           controllers.HomeController.index
# An example controller showing how to use dependency injection
GET     /count                      controllers.CountController.count
# An example controller showing how to write asynchronous code
GET     /message                    controllers.AsyncController.message

****************the error points on this line:************
GET     /comments                            
controllers.CommentsController.getComments(postId: Integer)

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file                       
controllers.Assets.versioned(path="/public", file: Asset)

完整的控制器代码:

package controllers;

import play.mvc.Controller;
import play.mvc.Result;

import services.CommentsDictionary;
import java.util.ArrayList;
import models.Comment;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.List;


@Singleton
public class CommentsController extends Controller {

private final CommentsDictionary commentsDic;

@Inject
public CommentsController() {
   this.commentsDic = new CommentsDictionary();
}


public List<Comment> getComments(int postId) {        
    List<Comment> comments = this.commentsDic.getComments(postId);        
    return comments;
}

}

1 个答案:

答案 0 :(得分:2)

您想要哪种内容的HTTP响应?

如果您想让Json简单地返回

ok(play.libs.Json.toJson(comments));

如果要显示这样的页面呈现模板:

ok(views.html.yourTemplate.render(comments));

一些进一步阅读的提示:

https://www.playframework.com/documentation/2.6.x/JavaActions#Results https://www.playframework.com/documentation/2.5.8/api/java/play/mvc/Results.html