zoukankan      html  css  js  c++  java
  • 调用WCF REST服务时,使用JSON

    四、JSON+ Wrapped

    我们最后来演示Json消息格式在Wrapped风格下具有怎样的结构,为此我们只需要将应用在Create操作方法上的WebInvokeAttribute特性的BodyStyle属性设置为Wrapped。

       [ServiceContract]
        public interface IEmployees
       {
            //其他成员
            [WebInvoke(UriTemplate = "/", Method = "POST", 
            RequestFormat = WebMessageFormat.Json, 
            ResponseFormat = WebMessageFormat.Json, 
            BodyStyle = WebMessageBodyStyle.Wrapped)]
            Employee Create(Employee employee);
       }
    

    如下面的代码所示,由于请求消息和回复消息采用Wrapped风格,表示Employee的JSON对象最终作为最终JSON对象的“employee”属性和“CreateResult”属性。(S1005)

    请求消息主体:
    {"employee":{"Department":"行政部","Grade":"G9","Id":"003","Name":"王五"}}
         
    回复消息主体:
    {"CreateResult":{"Department":"行政部","Grade":"G9","Id":"003","Name":"王五"}}
    

    以上内容摘自 蒋老师的博客:http://www.cnblogs.com/artech/archive/2012/02/06/wcf-rest-web-message-body-style.html

    下面是我自己遇到的问题:调用方传递过来的JSON数据,总是无法初始化为类实例,参数总是为null。后面才发现,如果使用

    WebMessageBodyStyle.Wrapped

    那么,JSON字符串里的最外层的属性,在上面的例子中是employee ,必须和服务中方法

    Employee Create(Employee employee)

    的参数相同。如下图所示:

  • 相关阅读:
    使用json-lib进行Java和JSON之间的转换
    ajax+json+java
    了解Json
    Ehcache RIM
    利用FreeMarker静态化网页
    Timer和TimerTask
    windows下memcache安装
    mac下安装YII
    php static 和self区别
    YII behaviors使用
  • 原文地址:https://www.cnblogs.com/xl0715/p/3433614.html
Copyright © 2011-2022 走看看