首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >struts2 2-rest插件:将json数据发送到PUT/POST

struts2 2-rest插件:将json数据发送到PUT/POST
EN

Stack Overflow用户
提问于 2012-10-10 06:12:53
回答 2查看 3.7K关注 0票数 1

我无法使用struts2-rest-plugin将Struts2 REST服务器发送JSON数据。

它适用于XML,但我似乎找不出正确的JSON格式来发送它。

有人有这方面的经验吗?

谢了,肖恩

更新:

抱歉我没说清楚。问题是,Struts2似乎没有映射我发送到控制器中的模型的JSON数据。

下面是代码:

主计长:

代码语言:javascript
复制
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

-Method: POST或PUT (尝试了这两种方法,POST映射用于创建(),并将映射放到update()中) -Header: Content: application/json

有效载荷:

代码语言:javascript
复制
{"clientFeature":{
        "feature": {
            "id": 2,
            "enabled": true,
            "description": "description1",
            "type": "type1"
        },
        "countries": ["SG"],
        "clientId": 10}
}

当我提出请求时,Struts2中的输出将记录如下:

代码语言:javascript
复制
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://localhost:8080/coreserviceswrapper/clientfeature.xml方法: POST/PUT内容-类型: text/xml

有效载荷:

代码语言:javascript
复制
<com.foo.bar.entity.ClientFeature>
<clientId>100</clientId>
<feature>
<description>test</description>
</feature>
</com.foo.bar.entity.ClientFeature>

输出:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-19 12:42:45

我遇到了这样的问题。奇怪,但通过将“clientFeature”改为“model”解决了

票数 0
EN

Stack Overflow用户

发布于 2014-11-26 07:25:12

我也遇到了同样的问题,我的环境是: Structs 2.3.16.3,Jquery 1.11,Struts- rest插件症状: post json数据,rest控制器不解析json数据到模型。解决方案:由于控制器是模型驱动的,浏览器客户端只需发布Json字符串就可以了。但是,似乎您必须强制jquery更改ajax调用的conenttype。

代码语言:javascript
复制
_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”=“application/json”放置或发布数据时,插件将使用Jsonhandler自动处理它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12813045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档