Skip to content

Instantly share code, notes, and snippets.

@krasserm
Created February 23, 2012 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krasserm/1891558 to your computer and use it in GitHub Desktop.
Save krasserm/1891558 to your computer and use it in GitHub Desktop.
JAXB-based XML and JSON APIs
object Application extends Controller with JaxbSupport {
val paths: String = // colon-separated list of packages containing JAXB-annotated classes ...
val config: JSONConfiguration = // ...
// can be used for JAXB-based JSON and XML processing
implicit val context = new JSONJAXBContext(config, paths)
...
}
def postPersonXml = Action(jaxb.parse.xml[Person]) { request =>
val person: Person = request.body
// ...
}
def postPersonJson = Action(jaxb.parse.json[Person]) { request =>
val person: Person = request.body
// ...
}
def postPerson = Action(jaxb.parse[Person]) { request =>
val person: Person = request.body
// ...
}
def getPersonXml = Action {
val person: Person = ...
Ok(JaxbXml(person))
}
def getPersonJson = Action {
val person: Person = ...
Ok(JaxbJson(person))
}
def getPerson = Action { implicit request =>
val person: Person = ...
Ok(Jaxb(person)) // content negotiation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment