Render output format (HTML, JSON, XML) depending on parameter?

前端 未结 3 918
傲寒
傲寒 2020-12-28 09:36

is there a good or proper way to render the output in Play Framework depending on a parameter? Example:

For HTML:

http://lo         


        
3条回答
  •  情歌与酒
    2020-12-28 10:26

    I wanted the user to be able to be viewed in the browser as html or as json so the accepts method did not work for me.

    I solved it by putting a generic renderMethod in a base class with the following style syntax

    public static Result requestType( )
    {
        if( request().uri().indexOf("json") != -1)
        {
            return ok(Json.toJson(request()));
        }
        else 
        {
            return ok("Got HTML request " + request() );
        }
    }
    

提交回复
热议问题