Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:10:09+00:00 2026-06-04T07:10:09+00:00

is there a good or proper way to render the output in Play Framework

  • 0

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

For HTML:

http://localhost:9000/user/get/5?v=HTML // should render HTML template

For JSON:

http://localhost:9000/user/get/5?v=JSON // should render JSON template

I think that a request interceptor could have the ability to achieve this, but I have no clue how to start or where to start 🙁

Or perhaps, write a general render method that reads the parameters and output as requested, but this seems to me like overkill?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:10:11+00:00Added an answer on June 4, 2026 at 7:10 am

    If /user/5?v=html and /user/5?v=json return two representations of the same resource, they should be the same URL, e.g. /user/5, according to the REST principles.

    On client side, you can use the Accept header in your requests to indicate which representation you want the server to send you.

    On server side, you can write the following with Play 2.1 to test the value of the Accept header:

    public static Result user(Long id) {
    
      User user = User.find.byId(id);
      if (user == null) {
        return notFound();
      }
    
      if (request().accepts("text/html")) {
        return ok(views.html.user(user));
      } else if (request().accepts("application/json")) {
        return ok(Json.toJson(user));
      } else {
        return badRequest();
      }
    }
    

    Note that the test against "text/html" should always be written prior to any other content type because browsers set the Accept header of their requests to */* which matches all types.

    If you don’t want to write the if (request().accepts(…)) in each action you can factor it out, e.g. like the following:

    public static Result user(Long id) {
      User user = User.find.byId(id);
      return representation(user, views.html.user.ref);
    }
    
    public static Result users() {
      List<User> users = User.find.all();
      return representation(users, views.html.users.ref);
    }
    
    private <T> Result representation(T resource, Template1<T, Html> html) {
      if (resource == null) {
        return notFound();
      }
    
      if (request().accepts("text/html")) {
        return ok(html.apply(resource));
      } else if (request().accepts("application/json")) {
        return ok(Json.toJson(resource));
      } else {
        return badRequest();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any good way of truncating text with plain HTML and CSS, so
Is there a good way (proper way, or effective way) to debug slow running
is there a good example of a source file containing Javadoc ? I can
Is there a good way in asp.net MVC to trace ModelState errors? IsValid returns
Is there a good way to get the debug information that Django provides when
Is there any way purely with CSS (or 'proper' markup) to style the 2nd
I was wondering if there's any proper way of reading RadioButton that's checked from
Are there any good technical solutions for extremely long term archiving of data, for
Is there a good tool that will look at a .NET assembly and tell
Is there a good development IDE for Groovy/Grails code completion under Linux?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.