[2.0] How to adjust content type and set headers from Java?

626 views
Skip to first unread message

jh

unread,
Dec 25, 2011, 5:37:43 PM12/25/11
to play-framework
How should one go and set the content type for a request result using
Java? Also, how to add custom headers there?

public static Result index()
{
return ok("foobar"); // how to change the content type to
application/foobar, for example?
}

jh

unread,
Dec 25, 2011, 6:08:20 PM12/25/11
to play-framework
Replying to my own question:

public static Result index()
{
response().setContentType("application/foobar");
response().setHeader("foo", "bar");
return ok("foobar");
}

Ivan Meredith

unread,
Dec 25, 2011, 6:35:01 PM12/25/11
to play-fr...@googlegroups.com
i believe you can also do ok("foobar").as("application/foobar"), at
least in the scala api you can...

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

jh

unread,
Dec 26, 2011, 9:16:28 AM12/26/11
to play-framework
Thanks for the reply,

Doesn't seem to be supported by the Java API and
ok("foobar").getWrappedResult().as("foobar") doesn't work either.


On Dec 26, 1:35 am, Ivan Meredith <i...@ivan.net.nz> wrote:
> i believe you can also do ok("foobar").as("application/foobar"), at
> least in the scala api you can...
>

Julien Richard-Foy

unread,
Dec 26, 2011, 11:09:07 AM12/26/11
to play-fr...@googlegroups.com
The right way to do that is by using "response().setHeader(…)" or "response().setContentType(…)"
See http://playframework.github.com/api/java/play/mvc/Http.Response.html

jh

unread,
Dec 26, 2011, 3:24:32 PM12/26/11
to play-framework
How about HTTP status codes? Is there a generic container that can be
given a custom status code?

Currently only the following seems to be supported:

Results.BadRequest A 400 BAD_REQUEST simple result.
Results.Forbidden A 403 FORBIDDEN simple result.
Results.InternalServerError A 500 INTERNAL_SERVER_ERROR simple result.
Results.NotFound A 404 NOT_FOUND simple result.
Results.Ok A 200 OK simple result.
Results.Redirect A 302 FOUND simple result.
Results.Todo A 501 NOT_IMPLEMENTED simple result.
Results.Unauthorized A 401 UNAUTHORIZED simple result.

On Dec 26, 6:09 pm, Julien Richard-Foy <j...@zenexity.com> wrote:
> The right way to do that is by using "response().setHeader(…)" or "response().setContentType(…)"
> Seehttp://playframework.github.com/api/java/play/mvc/Http.Response.html

Julien Richard-Foy

unread,
Dec 27, 2011, 4:11:01 AM12/27/11
to play-fr...@googlegroups.com
You can return custom status codes using the Status class:

return new Status(218); // Empty body

or

return new Status(218, "content", utf8);

I don’t understand why this API does not appear in the JavaDoc.
Anyway, this API is not consistent with the other ways to return
results. Maybe something like the following would be more appropriate?

return status(218);
return status(218, "content");
return status(218, "content", aCustomCharset);

Reply all
Reply to author
Forward
0 new messages