【问题标题】:Restlet POST Form to send to client resource via JSONRestlet POST 表单通过 JSON 发送到客户端资源
【发布时间】:2013-03-09 07:51:36
【问题描述】:

通过 JSON 将表单条目发送到另一个客户端资源时,我收到 415 错误。我下面代码中的目标 URI(“/message”)在不使用表单时有效(即用测试模拟对象点击“/message”)。

这是我获取表单值并发布到目标资源的代码。我错过了一些需要做的事情吗?

我正在使用以下内容:

  • Restlet:2.1 RC5
  • GAE:1.6.1

Form Restlet:

@Post
public void handlePost(Representation entity) {

    final Form webForm = new Form(entity);
    MessageEntity newMessage = new MessageEntity();

    String subject = webForm.getFirstValue("subject");
    String sendto = webForm.getFirstValue("email");
    String message = webForm.getFirstValue("message");

    newMessage.setCategoryID(subject);
    newMessage.setAccountID(sendto);
    newMessage.setMessageText(message);

    ClientResource cr = new ClientResource(getRootRef()+ "/message");
    cr.post(newMessage, MediaType.APPLICATION_JSON);
}

目标资源(“/message”)

   @Post("json")
   public void HandleRequest(MessageEntity messageEntity) {

   // Logic here

   }

如果您需要更多信息,请告诉我

谢谢!

【问题讨论】:

    标签: json google-app-engine post restlet


    【解决方案1】:

    我的代码与您的代码非常相似,可以正常工作。我也在运行类似版本的 Restlet 和 GAE。我的第一个问题是您的目标资源中是否还有其他 @Post 方法,因为有时排序很重要。

    这里有两个版本的代码,我有这个工作...... 1)

    public Representation postHandler() {    
      Reference commitsRef = new Reference(Consts.RESOURCE_BASE + "commitments/");    
      ClientResource commitsResource = new ClientResource(getContext(), commitsRef);
      ....
      Representation commitsRep = commitsResource.post(commitForm);
    

    将表单发布到同时处理 @Post("json") 和 @Post("form") 的 Target 资源

    2)

    public Representation doPostFromGet() {   
      Reference takeActRef = new Reference(Consts.RESOURCE_BASE + "commitment/"
          + commitmentId + "/userActs/");     
      ClientResource takeActResource = new ClientResource(getContext(), takeActRef);
      ...
      Representation takeActRep = takeActResource.post(newAct);
    

    这就是将 Java 对象发布到使用我称之为“Peierls 魔法”的表单中。看: http://tembrel.blogspot.com/2012/03/converting-forms-in-restlet-to-pojos.html 它允许您在 Target 中有一个 post() 并接受表单和 pojo。

    顺便说一句,如果您正在发帖以添加新消息,那么 url 应该是“/messages/”(复数) - 也许某处有错字? (不太可能,但我想我会提到它)。

    祝你好运,

    RB

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多