2015-04-02 159 views
1

將響應設爲空。RESTful - QueryParam中爲空

這裏是我的代碼

package com.javacodegeeks.enterprise.rest.jersey; 

import java.util.Date; 

import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.QueryParam; 
import javax.ws.rs.core.Response; 

@Path("/") 
public class HelloWorldREST { 

@POST 
@Path("/submitValue") 
public Response responseMsg(@QueryParam("name") String name,@QueryParam("email") String email,@QueryParam("date") Date date) {  
String output = date+email+name; 
System.out.println(output);  

return Response.status(200).entity(output).build(); 
} 

}

這裏是調用URL

Ext.Ajax.request({ 
method : 'post', 
url: 'rest/submitValue/', 
//success: someFn, 
//failure: otherFn, 
params: 
{ 
name: Ext.getCmp('name').getValue(), 
email : Ext.getCmp('email').getValue(), 
date : Ext.getCmp('date').getValue() 

} 
}); 

我正在檢查,當我發這個請求參數得以通過...所以沒有機會,他們的價值爲零。

回答

0

所有的休息方法,即HelloWorldREST.responseMsg不應該交的,因爲它沒有requestBody第一。它應該是get方法。永遠記住,如果你想發送數據作爲查詢參數(網址參數)總是使用get,如果你想發送數據在請求正文使用POST。

現在,第二件事情,因爲你想發送數據作爲查詢參數,你可以做到這一點兩種方式。確保您從查詢Ext.getCmp('name')。getValue()獲取值,您可以在Chrome開發人員控制檯中檢查其值。

Ext.Ajax.request({ 
      url: 'rest/submitValue?name=' + Ext.getCmp('name').getValue() + '&email=' + Ext.getCmp('email').getValue() + '&date=' + Ext.getCmp('date').getValue(), 
      method: 'GET', 
      success: function (response) { 

      }, 
      failure:function(response){ 

      } 
     }); 



Ext.Ajax.request({ 
      url: 'rest/submitValue?name=' + Ext.getCmp('name').getValue() + '&email=' + Ext.getCmp('email').getValue() + '&date=' + Ext.getCmp('date').getValue(), 
      method: 'GET', 
      params: { 
       name: Ext.getCmp('name').getValue(), 
       email : Ext.getCmp('email').getValue(), 
       date : Ext.getCmp('date').getValue() 
      } 
      success: function (response) { 

      }, 
      failure:function(response){ 

      } 
     }); 
0

您可以將數據發送到服務器中使用的數據:代替則params的:

Ext.Ajax.request({ 
method : 'post', 
url: 'rest/submitValue/', 
//success: someFn, 
//failure: otherFn, 
data: 
{ 
name: Ext.getCmp('name').getValue(), 
email : Ext.getCmp('email').getValue(), 
date : Ext.getCmp('date').getValue() 

} 
}); 

三樣東西我在這裏看到

  1. 您使用jQuery的哪個版本?如果< 1.9.0那麼你應該使用type:,而不是類型的method:

定義:從jQuery的網站:

type (default: 'GET') 
Type: String 
An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. 
  • 控制器(HelloWorldREST)沒有@Component@Controller註釋就可以了。

  • 從休息角度看,對於POST調用,您不應該使用查詢參數而是將數據作爲有效負載發送。並在您的控制器上添加@Consumes(MediaType.APPLICATION_JSON)

  • 這可能有助於...

    +0

    它將如何解決問題....... ??嘗試過...但仍然有同樣的問題。 – sparsh610 2015-04-02 18:34:39