我正在使用play框架,我想为来自POST方法的JSON创建自定义绑定程序.我试图使用@Global TypeBinder进行此操作,但这似乎仅适用于以形式编码的形式出现(我正在研究Binder.java,并且似乎没有考虑将POST主体用于绑定,并假定存在在参数中使用的名称前缀,用于数据).
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); } }推荐答案
按照源中的说明进行创建你提到过.该代码段是相关的.
Create it as described in the source you've mentioned. The snippet is relevant.
一个不明显的地方是,handleJson中唯一参数的名称必须为body.
One not obvious point is that the name of the sole parameter in handleJson has to be body.
更多推荐
如何为JSON主体创建自定义绑定器?
发布评论