2014-09-26 91 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/

希望它可以幫助你,如果 蒂埃裏