如何传递和消费的JSON参数/ REST风格的WCF服务?风格、参数、JSON、WCF

2023-09-04 03:58:05 作者:十年很久你们别走

我是一个初学者在RESTful服务。

I am a beginner at RESTful services.

我需要建立在客户端需要通过多达9个参数的接口。

I need to create an interface where the client needs to pass up to 9 parameters.

我会preFER传递的参数作为一个JSON对象。

I would prefer to pass the parameters as a JSON object.

举例来说,如果我的JSON是:

For instance if my JSON is:

'{
    "age":100,
    "name":"foo",
    "messages":["msg 1","msg 2","msg 3"],
    "favoriteColor" : "blue",
    "petName" : "Godzilla",
    "IQ" : "QuiteLow"
}'

如果我需要在下面到底执行服务器端方法:

And if I need to execute a server side method below in the end:

public Person FindPerson(Peron lookUpPerson)
{
Person found = null;
// Implementation that finds the Person and sets 'found'
return found;
}

问题(S): 我应该怎么做,从客户端调用与上面的JSON字符串? 我怎么可以创建一个签名和执行RESTful服务方式,该

Question(s): How should I make the call from the client-side with the above JSON string? And how can I create a signature and implementation of the RESTful service method that

在接受该JSON, 分析和反序列化到Person对象和 在通话/返回是FindPerson方法的返回值回客户端?

推荐答案

如果你想创建一个WCF操作来接收JSON输入,您需要定义映射到该输入数据的合同。有几个工具可以自动做到这一点,其中包括我写了一段时间后,在 http://jsontodatacontract.azurewebsites.net / (关于该工具如何写在这个博客帖子)。生成的这个类,你可以使用该工具:

If you want to create a WCF operation to receive that JSON input, you'll need to define a data contract which maps to that input. There are a few tools which do that automatically, including one which I wrote a while back at http://jsontodatacontract.azurewebsites.net/ (more details on how this tool was written at this blog post). The tool generated this class, which you can use:

// Type created for JSON at <<root>>
[System.Runtime.Serialization.DataContractAttribute()]
public partial class Person
{

    [System.Runtime.Serialization.DataMemberAttribute()]
    public int age;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string name;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string[] messages;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string favoriteColor;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string petName;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string IQ;
}

接下来,你需要定义一个操作合同接收。由于JSON需要去请求的身体,用最自然的HTTP方法是 POST ,所以你可以定义如下操作:方法是POST 和风格是裸(这意味着你的JSON直接映射到参数)。请注意,您甚至可以忽略方法 BodyStyle 属性,因为POST WebMessageBodyStyle.Bare 是它们的默认值,分别)。

Next, you need to define an operation contract to receive that. Since the JSON needs to go in the body of the request, the most natural HTTP method to use is POST, so you can define the operation as below: the method being "POST" and the style being "Bare" (which means that your JSON maps directly to the parameter). Notice that you can even omit the Method and BodyStyle properties, since "POST" and WebMessageBodyStyle.Bare are their default values, respectively).

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
public Person FindPerson(Peron lookUpPerson)
{
    Person found = null;
    // Implementation that finds the Person and sets 'found'
    return found;
}

现在,在方法你必须输入映射到 lookupPerson 。您将如何实现你的方法的逻辑是由你。

Now, at the method you have the input mapped to lookupPerson. How you will implement the logic of your method is up to you.

注释后更新的

调用(通过jQuery)使用JavaScript服务的一个例子可以在下面找到。

One example of calling the service using JavaScript (via jQuery) can be found below.

var input = '{
    "age":100,
    "name":"foo",
    "messages":["msg 1","msg 2","msg 3"],
    "favoriteColor" : "blue",
    "petName" : "Godzilla",
    "IQ" : "QuiteLow"
}';
var endpointAddress = "http://your.server.com/app/service.svc";
var url = endpointAddress + "/FindPerson";
$.ajax({
    type: 'POST',
    url: url,
    contentType: 'application/json',
    data: input,
    success: function(result) {
        alert(JSON.stringify(result));
    }
});