Is it possible to return objects from a WebService?

Instead of returning a common string, is there a way to return classic objects? If not: what are the best practices? Do you rearrange the object in xml and rebuild the object from the other side? What are the other features?

+6
web-services
source share
11 answers

As already mentioned, you can do this in .net through serialization. By default, all native types are serializable, so this happens automatically for you.

However, if you have complex types, you need to mark the object using the [Serializable] attribute. The same applies to complex types as properties.

So, for example, you need to have:

[Serializable] public class MyClass { public string MyString {get; set;} [Serializable] public MyOtherClass MyOtherClassProperty {get; set;} } 
+8
source share

If an object can be serialized in XML and can be described in WSDL, then yes, it is possible to return objects from a web service.

+5
source share

Yes: in .NET they call this serialization, where objects are serialized in XML and then restored by the consumer service back to the original object type or surrogate with the same data structure.

+3
source share

If possible, I transfer objects to XML - this means that the web service is more portable - I can access the service in any language, I just need to create a parser / object transposer in that language.

Since we have WSDL files describing this service, it is almost automated on some systems.

(For example, we have a server written in pure python that replaces a server written in C, a client written in C ++ / gSOAP, and a client written in Cocoa / Objective-C. We use soapUI as a test framework written in Java).

+2
source share

You can return objects from a web service using XML. But web services must be agnostic of the platform and operating system. Serializing an object makes it easy to save and retrieve an object from a stream of bytes, such as a file. For example, you can serialize a Java object, convert this binary stream (possibly through Base 64 encoding to a CDATA field), and pass it to the service client.

But the client could restore this object only if it was based on Java. In addition, a deep copy is required to serialize the object and restore it exactly. Deep copies can be expensive.

Your best route is to create an XML schema that represents the document and create an instance of that schema with specific objects.

+2
source share

.NET automatically does this with objects that can be serialized. I am sure that Java works the same.

Here's an article that talks about object serialization in .NET: http://www.codeguru.com/Csharp/Csharp/cs_syntax/serialization/article.php/c7201

+1
source share

@Brian: I don’t know how everything works in Java, but in .net objects they are serialized to XML, and not in base64 strings. Webservice publishes a wsdl file containing the definitions of the methods and objects needed for your web service.

I hope no one creates web services that just create a base64 string

+1
source share

Daniel Auger:
As others have said, this is possible. However, if both the service and the client use an object that has exactly the same domain behavior on both sides, you probably did not need the service in the first place.

Lomax: I have to disagree with this, as this is a somewhat narrow comment. Using a webservice that can serialize domain objects into XML means that it makes it easy for clients who work with the same domain objects, but it also means that these clients are limited using this particular web service you opened, and it also works in on the contrary, letting other clients not own their domain of objects, but still services through XML.

@Lomax: You described two scenarios. Scenario 1: The client translates the XML message back to the same domain object. I believe this is an "object return". In my experience, this is a bad choice, and I will explain it below. Scenario 2: The client rehydrates the xml message to something other than the same domain object: I am 100% behind this, however I do not believe that this returns a domain object. It does send a message or DTO.

Now let me explain why true / clean / non-serializing DTO objects via a web service is usually a bad idea. Statement: in order to do this, first of all, you must either be the owner of both the client and the service, or provide the client with a library to use so that they can re-fit the object back to its true type. Problem. This domain object as a type now exists and belongs to two semi-connected domains. Over time, behavior may need to be added to one domain that does not make sense in another domain, and this leads to pollution and potentially painful problems.

I usually use scenario 2. I use only scenario 1, when there is every reason for this.

I apologize for being so brief with my initial answer. Hope this clears me to some extent as I understand it. Lomax, it would seem, we agree;).

+1
source share

JSON is a fairly standard way to transfer objects over the network (as a subset of javascript). Many languages ​​have a library that converts JSON code into a native object - see, for example, simplejson in Python.

See the JSON webpage for additional libraries for using JSON.

+1
source share

As others have said, this is possible. However, if both the service and the client use an object with the same domain behavior on both sides, you probably do not need the service in the first place.

0
source share

As others have said, this is possible. However, if both the service and the client use an object that has exactly the same domain behavior on both sides, you probably did not need the service in the first place.

I must disagree with this, as this is a somewhat narrow comment. Using a web service that can serialize domain objects in XML means that it makes it easier for clients to work with the same domain objects, but it also means that these clients are limited to using this particular web service that you opened, and it also works the other way around, letting other clients not know your domain objects, but still interact with your service through XML.

0
source share

All Articles