java - play.data.Form<> not filling j.u.Map<> fields when deserializing request body into object -
i form in playframework have noticed strange when have class has field map<>
import org.codehaus.jackson.annotate.jsonignoreproperties; import org.codehaus.jackson.annotate.jsonproperty; import org.codehaus.jackson.annotate.jsonsetter; import org.codehaus.jackson.map.annotate.jsondeserialize; import java.util.hashmap; import java.util.linkedhashmap; import java.util.map; @jsonignoreproperties(ignoreunknown = true) public class gamestate { private string roomid = ""; private string woohoo = ""; @jsondeserialize(as = linkedhashmap.class, contentas = integer.class, keyas = string.class) private map<string, integer> players = new hashmap<string, integer>(); @jsonproperty("players") public map<string, integer> getplayers() { return players; } @jsonsetter("players") public gamestate setplayers(map<string, integer> players) { this.players = players; return this; } public string getroomid() { return roomid; } public gamestate setroomid(string roomid) { this.roomid = roomid; return this; } public string getwoohoo() { return woohoo; } public gamestate setwoohoo(string woohoo) { this.woohoo = woohoo; return this; } } in controller have following static form:
static form<gamestate> gameform = new form<gamestate>(gamestate.class); and have following action:
@bodyparser.of(bodyparser.json.class) public static result testaction() { form<gamestate> form = gameform.bindfromrequest(); if (!form.haserrors()) { gamestate s = form.get(); if (!s.getplayers().isempty()) { return ok(); } else { return badrequest("need players!"); } } else { return badrequest("wrong format!"); } } this de-serialize other field (list<> , set<> included when had classes such fields) not map<> fields. strange thing works for:
gamestate s = json.fromjson(request().body().asjson(), gamestate.class); here curl call use test action
curl -x post http://localhost:9000/game/end/1 --data '{"roomid": "someid", "woohoo": "something", "players" : { "oliver" : "0" } }' -h 'content-type: application/json' i using play 2.1. not seem matter if have @bodyparser.of() or not on action.
question : there way make form.get() work properly?
Comments
Post a Comment