For JSON, I would recommend that you consider this question. How to display JSON response in Play Framework v2.0 (latest build from GIT)
XML is much simpler because you can simply call the result with the code as follows:
Ok(Xml(xmlString))
But a cleaner way using this function is probably to write your own template under views/xml , for example mdo.scala.xml could be
@(mdo:MyDomainObject) <?xml version="1.0" encoding="utf-8"?> <MyDomainObject> <name>@mdo.name</name> <desc>@mdo.desc</desc> <kws> @mdo.keywords map { k=> <kw>k</kw> } </kws> </MyDomainObject>
Then in your controller
def c = Action { val o = MyDomainObject("mine", "for example", List("stack", "over", "flow")) Ok(views.xml.mdo(o)) }
Otherwise, you might have a similar toXml toJson function using the marshaling library
source share