2014-09-26 87 views
0

我可以做一个没有任何问题的GET。 当用POST请求尝试它,我得到这个消息:用于Java的Restlet框架的POST

内部服务器错误
服务器遇到阻止它完成请求的意外情况

我用它测试适用于Chrome的简单REST客户端扩展程序,但在实际应用程序中可以看到相同的消息。

这是我的帖子:

@Post 
    public StringRepresentation pepe(Representation entity) { 
     StringRepresentation result = this.users(); 

     // Parse the given representation and retrieve data 
     Form form = new Form(entity); 
     String action = form.getFirstValue("action"); 

     if(action.equals("add")){ 
     //nothing 
     } 

     Db.closeConnection(); 

     return result; 
    } 

这是我@Get正常工作:

@Get 
    public StringRepresentation pepe() { 
     String action = getQuery().getValues("action"); 
     StringRepresentation result = null; 

     result = this.users(); 

     Db.closeConnection(); 

     return result; 
    } 

而且有趣的是:每当我删除的条件if(action.equals("add")){(这是空的里面)开机自检正常工作。 这将工作:

@Post 
    public StringRepresentation pepe(Representation entity) { 
     StringRepresentation result = this.users(); 

     // Parse the given representation and retrieve data 
     Form form = new Form(entity); 
     String action = form.getFirstValue("action"); 

     Db.closeConnection(); 

     return result; 
    } 

这是怎么回事?看起来很随意!

+1

检查你的“行动”变量不为空。你可能有一个你没有看到的NullPointerException。 – Claudio 2014-09-26 15:41:46

+0

是的,你是对的。 – Alvaro 2014-09-26 16:15:23

回答

0

是的,如果您在表单有效内没有条目action,您的变量action将为空。

你可以注意到的Restlet提供了一种方法getFirstValue有默认值参数:

String action = form.getFirstValue("action", "defaultActionValue"); 

这可以帮助你不要有NullPointerException

否则,您似乎尝试对方法POST执行多个操作。我认为这篇博文可以给你一些额外的提示:https://templth.wordpress.com/2015/03/20/handling-multiple-actions-for-a-post-method/

希望它可以帮助你,如果 蒂埃里