return value from java controller












0















I'm new with java controllers. I'm working on Play framework and I'm trying to return an ArrayList from the controller, and it gives errors.
What is the right way to do so? My code is right below.



Another question, I would like to write the simplest page with html and javascript angular functionality which call the Play framework controller. Is there a simple platform like Play framework but for the front side?



this method:



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


gives the following error:



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)


and this way:



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


gives this error:



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


pointing to the routes file on the marked line:



# 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)


The full controller code:



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;
}

}









share|improve this question

























  • Can you share result class?

    – GauravRai1512
    Nov 17 '18 at 13:41











  • What do you mean by result class?

    – Dolev
    Nov 17 '18 at 13:42











  • In you first method you have mention return type Result .

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Can you share full code?

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Result is default object of java. It's not my object. Result is used to wrap simple objects (as string) as answer from java controllers. This is what I know about it.

    – Dolev
    Nov 17 '18 at 13:56
















0















I'm new with java controllers. I'm working on Play framework and I'm trying to return an ArrayList from the controller, and it gives errors.
What is the right way to do so? My code is right below.



Another question, I would like to write the simplest page with html and javascript angular functionality which call the Play framework controller. Is there a simple platform like Play framework but for the front side?



this method:



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


gives the following error:



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)


and this way:



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


gives this error:



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


pointing to the routes file on the marked line:



# 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)


The full controller code:



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;
}

}









share|improve this question

























  • Can you share result class?

    – GauravRai1512
    Nov 17 '18 at 13:41











  • What do you mean by result class?

    – Dolev
    Nov 17 '18 at 13:42











  • In you first method you have mention return type Result .

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Can you share full code?

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Result is default object of java. It's not my object. Result is used to wrap simple objects (as string) as answer from java controllers. This is what I know about it.

    – Dolev
    Nov 17 '18 at 13:56














0












0








0


1






I'm new with java controllers. I'm working on Play framework and I'm trying to return an ArrayList from the controller, and it gives errors.
What is the right way to do so? My code is right below.



Another question, I would like to write the simplest page with html and javascript angular functionality which call the Play framework controller. Is there a simple platform like Play framework but for the front side?



this method:



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


gives the following error:



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)


and this way:



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


gives this error:



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


pointing to the routes file on the marked line:



# 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)


The full controller code:



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;
}

}









share|improve this question
















I'm new with java controllers. I'm working on Play framework and I'm trying to return an ArrayList from the controller, and it gives errors.
What is the right way to do so? My code is right below.



Another question, I would like to write the simplest page with html and javascript angular functionality which call the Play framework controller. Is there a simple platform like Play framework but for the front side?



this method:



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


gives the following error:



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)


and this way:



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


gives this error:



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


pointing to the routes file on the marked line:



# 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)


The full controller code:



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;
}

}






javascript java angular playframework controller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 14:16







Dolev

















asked Nov 17 '18 at 13:32









DolevDolev

13




13













  • Can you share result class?

    – GauravRai1512
    Nov 17 '18 at 13:41











  • What do you mean by result class?

    – Dolev
    Nov 17 '18 at 13:42











  • In you first method you have mention return type Result .

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Can you share full code?

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Result is default object of java. It's not my object. Result is used to wrap simple objects (as string) as answer from java controllers. This is what I know about it.

    – Dolev
    Nov 17 '18 at 13:56



















  • Can you share result class?

    – GauravRai1512
    Nov 17 '18 at 13:41











  • What do you mean by result class?

    – Dolev
    Nov 17 '18 at 13:42











  • In you first method you have mention return type Result .

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Can you share full code?

    – GauravRai1512
    Nov 17 '18 at 13:52











  • Result is default object of java. It's not my object. Result is used to wrap simple objects (as string) as answer from java controllers. This is what I know about it.

    – Dolev
    Nov 17 '18 at 13:56

















Can you share result class?

– GauravRai1512
Nov 17 '18 at 13:41





Can you share result class?

– GauravRai1512
Nov 17 '18 at 13:41













What do you mean by result class?

– Dolev
Nov 17 '18 at 13:42





What do you mean by result class?

– Dolev
Nov 17 '18 at 13:42













In you first method you have mention return type Result .

– GauravRai1512
Nov 17 '18 at 13:52





In you first method you have mention return type Result .

– GauravRai1512
Nov 17 '18 at 13:52













Can you share full code?

– GauravRai1512
Nov 17 '18 at 13:52





Can you share full code?

– GauravRai1512
Nov 17 '18 at 13:52













Result is default object of java. It's not my object. Result is used to wrap simple objects (as string) as answer from java controllers. This is what I know about it.

– Dolev
Nov 17 '18 at 13:56





Result is default object of java. It's not my object. Result is used to wrap simple objects (as string) as answer from java controllers. This is what I know about it.

– Dolev
Nov 17 '18 at 13:56












1 Answer
1






active

oldest

votes


















2














Which content type of HTTP response do you want?



If you want Json simply return



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


If you want show page render template like this:



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


Some tips for further reading:



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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351722%2freturn-value-from-java-controller%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Which content type of HTTP response do you want?



    If you want Json simply return



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


    If you want show page render template like this:



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


    Some tips for further reading:



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






    share|improve this answer




























      2














      Which content type of HTTP response do you want?



      If you want Json simply return



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


      If you want show page render template like this:



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


      Some tips for further reading:



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






      share|improve this answer


























        2












        2








        2







        Which content type of HTTP response do you want?



        If you want Json simply return



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


        If you want show page render template like this:



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


        Some tips for further reading:



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






        share|improve this answer













        Which content type of HTTP response do you want?



        If you want Json simply return



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


        If you want show page render template like this:



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


        Some tips for further reading:



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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 13:13









        TijkijikiTijkijiki

        338211




        338211
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351722%2freturn-value-from-java-controller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini