-
Notifications
You must be signed in to change notification settings - Fork 19
Play2 integration
giabao edited this page Oct 4, 2013
·
4 revisions
object ApplicationBuild extends Build {
val appName = "play2-xmlsoap"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
"play2.tools.xml" %% "xmlsoap-ersatz" % "0.1-SNAPSHOT"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
resolvers += ("mandubian-mvn snapshots" at "https://github.com/mandubian/mandubian-mvn/raw/master/snapshots")
)
}
package controllers
import play.api._
import play.api.mvc._
import play2.tools.xml._
import play2.tools.xml.DefaultImplicits._
object Application extends Controller {
case class Foo(id: Long, name: String, age: Option[Int])
implicit object FooXMLF extends XMLFormatter[Foo] {
def read(x: xml.NodeSeq): Option[Foo] = {
for(
id <- EXML.fromXML[Long](x \ "id");
name <- EXML.fromXML[String](x \ "name");
age <- EXML.fromXML[Option[Int]](x \ "age")
) yield(Foo(id, name, age))
}
def write(f: Foo, base: xml.NodeSeq): xml.NodeSeq = {
<foo>
<id>{ f.id }</id>
<name>{ f.name }</name>
{ EXML.toXML(f.age, <age/>) }
</foo>
}
}
def index = Action {
Ok("coucou")
}
def foo = Action(parse.xml) { request =>
EXML.fromXML[Foo](request.body).map { foo =>
Ok(EXML.toXML(foo))
}.getOrElse{
BadRequest("Expecting Foo XML data")
}
}
}