简体   繁体   English

从SOAP到REST

[英]Going from SOAP to REST

I have a bunch of WCF SOAP services that serve my WP7 apps. 我有一堆可为WP7应用程序提供服务的WCF SOAP服务。 Now I'm writing Android versions of those WP7 apps but SOAP support is practically nonexistent in Android so I must use REST for these Android apps. 现在,我正在编写这些WP7应用程序的Android版本,但是Android中几乎不存在SOAP支持,因此我必须对这些Android应用程序使用REST。

So, my plan is to duplicate my WCF SOAP services in ASP.NET Web API. 因此,我的计划是在ASP.NET Web API中复制WCF SOAP服务。 I am new to RESTful services. 我是RESTful服务的新手。

Now that I'm getting into the details I think I'm appreciating SOAP a little more. 现在,我进入了细节,我认为我会更加欣赏SOAP。

For instance one of my SOAP methods takes, among other arguments, a "TripLocker_s" object. 例如,除其他参数外,我的SOAP方法之一还带有一个“ TripLocker_s”对象。 (SOAP definition pasted below). (下面粘贴了SOAP定义)。 As you can see a TripLocker_s object is an array TripLegModel_s objects. 如您所见,TripLocker_s对象是一个TripLegModel_s对象数组。 My various service methods pass all sorts of complex objects as arguments. 我的各种服务方法都将各种复杂的对象作为参数传递。

When I review any Android tutorials having to do with using HTTP services, nothing more complex than a string and maybe an integer is ever discussed as an argument. 当我回顾任何与使用HTTP服务有关的Android教程时,没有比字符串更复杂的了,也许还有整数作为参数被讨论过。 I can't find any information on passing complex objects. 我找不到有关传递复杂对象的任何信息。

How do I pass complex objects as arguments with Web api? 如何使用Web API将复杂对象作为参数传递? Is there a way, for instance, to take a TripLocker_s object and serialize it as http parms (and then back again on the server?) 例如,是否有办法获取TripLocker_s对象并将其序列化为http parms(然后再次返回到服务器上?)。

Thanks, Gary 谢谢,加里

    [DataContract]
public class TripLegModel_s
{
    [DataMember]
    public string ourDirection;  //SE, SW, SSW, etc.
    [DataMember]
    public double longitude;
    [DataMember]
    public double latitude;
    [DataMember]
    public double altitude;
    [DataMember]
    public DateTimeOffset TimeStamp;
    [DataMember]
    public double speed;
}

[DataContract]
public class TripLocker_s
{
    [DataMember]
    public ObservableCollection<TripLegModel_s> BreadCrums = new ObservableCollection<TripLegModel_s>();

Thanks, 谢谢,

No reason you can't pass complex objects as JSON or XML in RESTful APIs via RESTful (http) verbs (get, post, put, etc...). 没有理由不能通过RESTful(http)动词(获取,发布,放置等)在RESTful API中将复杂对象作为JSON或XML传递。 JSON is more web (javascript) friendly and RESTful/hypermedia APIs are typically chosen for web apps. JSON与Web(javascript)更友好,并且通常为Web应用选择RESTful /超媒体API。 Since it's so web friendly, if you choose it for mobile android consumption the API is also more consumable for web, iOS, win phone etc... Javascript (and therefore JSON) is becoming the language of choice for the web. 由于它非常适合网络使用,因此如果您选择将其用于移动android消费,则该API也更适合用于网络,iOS,Win Phone等。Javascript(以及JSON)正成为网络的首选语言。

If you're creating, you post the serialized object in the body. 如果要创建,则将序列化的对象发布到主体中。
See this post for a full example: restful-like CRUD operation url pattern for nested model 有关完整示例,请参见此帖子: 嵌套模型的类似Restful的CRUD操作网址模式

If you're using WCF, consider webapi, see http://www.asp.net/web-api 如果您使用的是WCF,请考虑使用webapi,请参见http://www.asp.net/web-api

Specifically, checkout: http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations 具体来说,结帐: http : //www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

For example, in the webapi CRUD sample, it works with a products object: 例如,在webapi CRUD示例中,它与product对象一起使用:

namespace ProductStore.Models
{
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }
}

Finally, checkout this read on thinking more RESTful (as opposed to RPC or SOAP) http://blog.steveklabnik.com/posts/2011-07-03-nobody-understands-rest-or-http 最后,请阅读此文章,了解更多关于RESTful的知识(与RPC或SOAP相对) http://blog.steveklabnik.com/posts/2011-07-03-nobody-understands-rest-or-http

RE: "My plan is to duplicate my WCF SOAP services in ASP.NET Web API." RE:“我的计划是在ASP.NET Web API中复制WCF SOAP服务。”

SUGGESTION: Don't even think about it! 建议:甚至不要考虑 There's no need ... you should be able to create REST-based web services just fine. 不需要...您应该能够创建基于REST的Web服务。 That's where the world is going ... and Microsoft is going along with it. 那就是世界前进的方向……而微软也在与时俱进。 It should't be an issue. 这不应该是一个问题。

And please don't get hung up on "everything's gotta be an object". 并且,请不要挂在“一切都必须成为对象”上。 It DOESN'T . 没有 In particular, why bog down your Android app with an (arguably unnecessary) GSON library, if a simple JSON message will work just as well? 特别是,如果简单的JSON消息也能正常工作,为什么还要使用一个(可能是不必要的)GSON库来中断Android应用程序呢?

Also, if you're not currently a "Java Guy", you might want to consider coding your Android stuff in a higher-level framework like PhoneGap: 另外,如果您当前不是“ Java Guy”,则可能需要考虑在诸如PhoneGap的高级框架中编码Android内容:

Just a suggestion! 只是一个建议!

1) Why can't you use SOAP if you want to? 1)如果需要,为什么不能使用SOAP? What specific problems are you having? 您有什么具体问题?

2) Why would anybody want to use SOAP if they could use REST instead? 2)为什么会有人使用SOAP,如果他们可以使用REST呢?

3) Are you familiar with JSON ? 3)您熟悉JSON吗?

Your Android is a web service client, correct? 您的Android是网络服务客户端,对吗? What/where is the web service itself, and is it using SOAP, JSON or "something else"? Web服务本身在哪里/在哪里?它是使用SOAP,JSON还是“其他”?

Please check out this instructive tutorial on REST: 请查看有关REST的指导性教程:

These other links might be useful: 这些其他链接可能会有用:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM