如何将请求正文绑定到操作方法参数?

发布于 2024-12-08 09:28:25 字数 613 浏览 0 评论 0原文

我了解 Play 如何将 URI 段或参数绑定到操作方法参数。我还看到了如何访问上传的文件。

但我仍在寻找将 PUT 或 POST 请求的请求实体绑定到方法参数的方法。

假设请求是这样的

PUT /blog/entries/67

Content-Type: application/atom+xml

<entry>...</entry>

我想将其绑定到条目参数:

public static void (String entryId, Entry entry) {
    // entryId being 67
    // entry being the deserialized Atom payload (e.g. using Apache Abdera parser)

   backend.updateEntry(67,entry);

   // AtomPub's 'canonical' response is the updated entry.
   render(entry) 

}

两个问题:

这样的东西有效吗?

在哪里可以找到有关如何创建解串器的文档?

I understand how Play binds URI segments or parameters to action method parameters. I also saw how an uploaded file can be accessed.

But I am still looking for the way to bind the request entity of a PUT or POST request to a method parameter.

Suppose the request is sth like

PUT /blog/entries/67

Content-Type: application/atom+xml

<entry>...</entry>

And I would like to bind that to an entry parameter:

public static void (String entryId, Entry entry) {
    // entryId being 67
    // entry being the deserialized Atom payload (e.g. using Apache Abdera parser)

   backend.updateEntry(67,entry);

   // AtomPub's 'canonical' response is the updated entry.
   render(entry) 

}

Two questions:

Does something like this work?

Where do I find the documentation of how to create the deserializer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2024-12-15 09:28:25

查看 Play 网站上的自定义绑定文档。

http://www.playframework.org/documentation/1.2.3/controllers#custombinding

我认为您正在寻找的是 play.data.binding.TypeBinder,它允许您自定义 Play 绑定控制器中某些对象的方式。

更新:

查看游戏组,Guillaume 发布了以下代码,用于在 POST 正文中处理 JSON,因此可以轻松地对其进行修改以从atom+xml 输入获取 XML。它使用 BinderPlugin,而不是 TypeBinder,这允许您执行更强大的绑定操作。

package plugins;

import play.*;
import play.mvc.*;

import com.google.gson.*;

import java.util.*;
import java.lang.reflect.*;
import java.lang.annotation.*;

public class BinderPlugin extends PlayPlugin {

    public Object bind(String name, Class clazz, Type type, Annotation[] annotations, Map<String, String[]> params) {
        if(Http.Request.current().contentType.equals("application/json")) {
            JsonObject json = new JsonParser().parse(Scope.Params.current().get("body")).getAsJsonObject();
            if(clazz.equals(String.class)) {
                return json.getAsJsonPrimitive(name).getAsString();
            }
        }
        return null;
    }

}

Take a look at the Custom Binding documentation on the Play site.

http://www.playframework.org/documentation/1.2.3/controllers#custombinding

I think what you are looking for is play.data.binding.TypeBinder, which allows you to customise the way Play binds certain objects in your controller.

Update:

Looking at the play groups, Guillaume has posted the following code for handling JSON in the body of a POST, so this could easily be adapted to get XML from an atom+xml input. It uses a BinderPlugin, rather than the TypeBinder, which allows you to do more pwerful binding operations.

package plugins;

import play.*;
import play.mvc.*;

import com.google.gson.*;

import java.util.*;
import java.lang.reflect.*;
import java.lang.annotation.*;

public class BinderPlugin extends PlayPlugin {

    public Object bind(String name, Class clazz, Type type, Annotation[] annotations, Map<String, String[]> params) {
        if(Http.Request.current().contentType.equals("application/json")) {
            JsonObject json = new JsonParser().parse(Scope.Params.current().get("body")).getAsJsonObject();
            if(clazz.equals(String.class)) {
                return json.getAsJsonPrimitive(name).getAsString();
            }
        }
        return null;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文