javapostplayframeworkplayframework-1.x

How can I retrieve the body as JSON Play framework 1.5


I`m using Play Framework 1.5 and I'm new in this framework. How can I retrieve the body as JSON when i sent a request body with JSON:

{
  "inputNo": 111111,
  "name": "検証 太郎"
}

Solution

  • AFAIK there is no built in JSON binding support in Play1x. You can implement binding process manually by using @Before annotation in controller class. In the @Before annotated method, get the json from request body (params.get("body")), parse it using GSON or any other JSON library, and store the pojo in the request object (request.args.put(name, pojo)). Later in the controller method get the pojo from request args (request.args.get(name)). You can define custom annotations to limit this behaviour.

    A good implementation would be creating a base controller class with @Before annotation and making this process generic using types.