如何为JSON主体创建自定义绑定器?

编程入门 行业动态 更新时间:2024-10-24 16:33:13
本文介绍了如何为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主体创建自定义绑定器?

本文发布于:2023-11-16 16:23:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   绑定   何为   主体   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!