Reputation: 548
I did not find a example detailing out the format of a method that accepts post request and a corresponding functional test for the same. Below is what I am doing. The controller method gets null json string value. I looked at the binder option, but I feel the below approach to be easy and trying to make this work. Am I missing something trivial?
play v 1.2.4
routes
/v1/xyzs Application.createXyz(format:'json')
Controller
public static void createXyz(String xyzJson)
{
Xyz xyz = new Gson().fromJson(xyzJson, Xyz.class);
Xyz savedXyz = xyz.save();
render();
}
Functional Test
@Test
public void createXyzTest()
{
Xyz xyz = new Xyz("id", 12345, "Summary", "Main");
String body = new Gson().toJson(xyz);
Response response = POST("/v1/xyzs", "application/json", body);
assertStatus(201, response);
}
UPDATE: Below code in functional test worked.
Xyz xyz = new Xyz("id", 12345, "Summary", "Main");
String body = new Gson().toJson(xyz);
Map<String,String> params = new HashMap<String,String>();
params.put("xyzJson", body);
Response response = POST("/v1/xyzs",params);
Upvotes: 2
Views: 830
Reputation: 14373
What if String body = String.format("{\"xyzJson\": %s}", new Gson().toJson(xyz));
Upvotes: 1