struts2-rest-plugin:将json数据发送到PUT / POST

时间:2012-10-10 06:12:54

标签: java json rest struts2 struts2-json-plugin

我试图使用struts2-rest-plugin向Struts2 REST服务器发送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;
    }
}

这是我发出请求的网址:

  

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

-Method: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)

2 个答案:

答案 0 :(得分:1)

我也遇到同样的问题,我的环境是: 结构2.3.16.3,Jquery 1.11,Struts-rest-plugin 症状:发布json数据,其余控制器不会将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 model = {“id”:“2”,     “名”:“NAME2”     “作家”:“author2”     “钥匙”: “KEY2”     }

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

答案 1 :(得分:0)

我遇到了这样的问题。奇怪,但通过将名称'clientFeature'更改为'model'

得到解决