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

2019-05-11 05:03发布

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条回答
趁早两清
2楼-- · 2019-05-11 05:55

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.

查看更多
登录 后发表回答