How do I create a custom binder for a JSON body?

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

I am working with the play framework and I would like to create a custom binder for JSON that comes from a POST method. I was trying to do that with a @Global TypeBinder, but that seems to only work for when things come as form encoded (I was digging through Binder.java and it does not seem to consider the POST body for binding and assumes that there is a name prefix in params to use for data).

Right now I am taking the approach referenced here that appears to be out of date, so I am assuming things have changed.

public class GsonBinder implements TypeBinder<JsonObject> {      public Object bind(String name, Annotation[] antns, String value, Class type) throws Exception {         return new JsonParser().parse(value);     } }

回答1:

Create it as described in the source you've mentioned. The snippet is relevant.

One not obvious point is that the name of the sole parameter in handleJson has to be body.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!