2012-10-10 67 views
0

我被困在試圖通過使用struts2-rest-plugin的Struts2 REST服務器發送JSON數據。struts2-rest-plugin:將json數據發送到PUT/POST

它與XML,但我似乎無法找出正確的JSON格式發送進去。

任何人有這種經驗嗎?

感謝, 肖恩

更新:

對不起,我不清楚。問題在於Struts2似乎並沒有將我發送給我的模型的JSON數據映射到控制器中。

下面的代碼:

控制器:

public class ClientfeatureController extends ControllerParent implements ModelDriven<Object> { 
    private ClientFeatureService clientFeatureService; 

    private ClientFeature clientFeature = new ClientFeature(); 
    private List<ClientFeature> clientFeatureList; 

    //Client ID 
    private String id; 

    public ClientfeatureController() { 
     super(ClientfeatureController.class); 
    } 

    @Override 
    public Object getModel() { 
     return (clientFeatureList != null ? clientFeatureList : clientFeature); 
    } 

    /** 
    * @return clientFeatureList through Struts2 model-driven design 
    */ 
    public HttpHeaders show() { 
     //logic to return all client features here. this works fine.. 

     //todo: add ETag and lastModified information for client caching purposes 
     return new DefaultHttpHeaders("show").disableCaching(); 
    } 

    // PUT request 
    public String update() { 
     logger.info("client id: " + clientFeature.getClientId()); 
     logger.info("clientFeature updated: " + clientFeature.getFeature().getDescription()); 

     return "update"; 
    } 

    public HttpHeaders create() { 
     logger.info("client id: " + clientFeature.getClientId()); 
     logger.info("feature description: " + clientFeature.getFeature().getDescription()); 
     return new DefaultHttpHeaders("create"); 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public void setClientFeatureService(ClientFeatureService clientFeatureService) { 
     this.clientFeatureService = clientFeatureService; 
    } 

    public List<ClientFeature> getClientFeatureList() { 
     return clientFeatureList; 
    } 

    public void setClientFeatureList(List<ClientFeature> clientFeatureList) { 
     this.clientFeatureList = clientFeatureList; 
    } 

    public ClientFeature getClientFeature() { 
     return clientFeature; 
    } 

    public void setClientFeature(ClientFeature clientFeature) { 
     this.clientFeature = clientFeature; 
    } 
} 

這就是我正在做的請求的網址:

..http://本地主機:8080/coreserviceswrapper /clientfeature.json

-Method:POST或PUT(同時嘗試POST映射到創建(),然後把地圖更新()) -Header:內容類型:應用程序/ JSON

有效載荷:

{"clientFeature":{ 
     "feature": { 
      "id": 2, 
      "enabled": true, 
      "description": "description1", 
      "type": "type1" 
     }, 
     "countries": ["SG"], 
     "clientId": 10} 
} 

而在Struts2的日誌輸出的時候我提出的要求:

1356436 [http-bio-8080-exec-5] WARN net.sf.json.JSONObject - Tried to assign property clientFeature:java.lang.Object to bean of class com.foo.bar.entity.ClientFeature 
1359043 [http-bio-8080-exec-5] INFO com.foo.bar.rest.ClientfeatureController - client id: null 

我還要補充一點,XML請求工作得很好:

網址:..http://本地主機:8080/coreserviceswrapper/clientfeature.xml 我的ThOD:POST/PUT 內容類型:文本/ XML

有效載荷:

<com.foo.bar.entity.ClientFeature> 
<clientId>100</clientId> 
<feature> 
<description>test</description> 
</feature> 
</com.foo.bar.entity.ClientFeature> 

輸出:

1738685 [http-bio-8080-exec-7] INFO com.foo.bar.rest.ClientfeatureController - client id: 100 
1738685 [http-bio-8080-exec-7] INFO com.foo.bar.rest.ClientfeatureController - feature description: test 
1738717 [http-bio-8080-exec-7] INFO org.apache.struts2.rest.RestActionInvocation - Executed action [/clientfeature!create!xml!200] took 1466 ms (execution: 1436 ms, result: 30 ms) 
+1

分享一些代碼來了解您的問題 –

+0

有幾種JSON格式? –

+0

嗨,大家好,我更新了我的問題,希望更清楚。謝謝。 – shaunlim

回答

0

我得到了這樣的問題。奇怪,但後來改變了名稱「clientFeature」到「模型」

1

我也遇到同樣的問題解決了,我的環境是: 的Structs 2.3.16.3,jQuery的1.11,Struts的休息,插件 症狀:後JSON數據, rest控制器不會將json數據解析爲模型。 解決方案: 由於控制器是modeldriven,瀏覽器客戶端只需發佈Json字符串就可以了。但似乎你必須強制jQuery改變ajax調用的conenttype。

_self.update= function(model, callback) { 
      $.ajax({ 
       beforeSend: function(xhrObj){ 
       xhrObj.setRequestHeader("Content-Type","application/json"); 
       xhrObj.setRequestHeader("Accept","application/json"); 
      }, 
       type: 'PUT', 
       url: this.svrUrl+"/"+ model.id + this.extension, 
       data: JSON.stringify(model), // '{"name":"' + model.name + '"}', 
       //contentType: this.contentType, 
       //dataType: this.dataType, 
       processData: false,     
       success: callback, 
       error: function(req, status, ex) {}, 
       timeout:60000 
      }); 
     }; 

模型數據格式是: VAR模型= { 「ID」: 「2」, 「名稱」: 「NAME2」, 「作者」: 「author2」, 「鍵」:」 key2「 }

當你放置或發佈數據」內容類型「=」application/json「時,插件將自動處理它與Jsonhandler。