如何为JSON主体创建自定义绑定器? [英] How do I create a custom binder for a JSON body?

查看:84
本文介绍了如何为JSON主体创建自定义绑定器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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主体创建自定义绑定器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆