1

struts2-rest-pluginを使用してStruts2RESTサーバーからJSONデータを送信しようとして立ち往生しています。

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;
    }
}

これは私がリクエストしているURLです:

..http:// localhost:8080 / coreserviceswrapper / clientfeature.json

-メソッド:POSTまたはPUT(両方を試し、POSTマップをcreate()に、PUTマップをupdate()に)-ヘッダー:Content-Type:application / 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リクエストは問題なく機能することを付け加えておきます。

URL:.. http:// localhost:8080 / coreserviceswrapper / clientfeature.xmlメソッド:POST / PUTコンテンツタイプ:text / 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)
4

2 に答える 2

1

私も同じ問題に遭遇します、私の環境は次のとおりです:Structs 2.3.16.3、Jquery 1.11、Struts-rest-plugin症状:jsonデータを投稿し、レストコントローラーがjsonデータをモデルに解析しません。解決策:コントローラーはモデル駆動型であるため、ブラウザークライアントは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  
          });  
      };  

モデルのデータ形式は次のとおりです。varmodel={"id": "2"、 "name": "name2"、 "author": "author2"、 "key": "key2"}

「Content-Type」="application/ json"でデータを配置または投稿すると、プラグインはJsonhandlerでデータを自動的に処理します。

于 2014-11-26T07:25:12.870 に答える
0

私はそのような問題を抱えました。奇妙ですが、「clientFeature」という名前を「model」に変更することで解決しました

于 2013-11-19T12:42:45.633 に答える