1

我一直在尝试使用 struts2-rest-plugin 将 JSON 数据发送到 Struts2 REST 服务器。

它适用于 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()) -Header: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 症状:post json data,rest controller not parse json data to model。解决方案:由于控制器是模型驱动的,浏览器客户端只需发布 Json 字符串即可。但似乎您必须强制 jquery 更改 ajax 调用的内容类型。

_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 model = {"id":"2", "name":"name2", "author":"author2", "key":"key2" }

当您使用“Content-Type”="application/json" 放置或发布数据时,插件将使用 Jsonhandler 自动处理它。

于 2014-11-26T07:25:12.870 回答
0

我遇到了这样的问题。奇怪但通过将名称“clientFeature”更改为“模型”解决了

于 2013-11-19T12:42:45.633 回答