Four frequently asked questions in WebService development (2)
WebService development often encounters problems such as WebService and method overloading, circular referencing, data being worn, and so on. This article will give you some good solutions.
AD:WOT2014: User labeling system and user data Operation training session
Question three: circular references
Let's look at an example first. Here is the interface for WebService:
1 @WebService 2 public interface Ihello { 3 4 @WebMethod 5 Public String SayHello (Teacher Teacher); 6 7} 8 9 public class Teacher { Student[] students; 11 //getters and Setters 13 ... 14} 15 public class Student { + Private Teacher Teacher; 18 //getters and Setters 20 ... 21} |
Note that teacher and student are one-to-many "two-way" relationships. In this case, we can think about how to convert a teacher object into XML?
You might think of the following answer:
1 <teacher> 2 <students> 3 <teacher> 4 <students> 5 <teacher> 6 <students> 7 ... 8 </students> 9 ... Ten </teacher> </students> 12 ... </teacher> </students> 15 <students> 17 ... </students> 19 ... </teacher> |
See, XML is going to go into a "dead loop". The root of the problem is a circular reference between objects. This problem usually throws an exception before the client sends the WebService request because the object cannot be converted to a transportable XML.
Question four: Leviathan
Let's look at an example first, and here's the WebService interface:
1 @WebService 2 public interface Ihello {
4 @WebMethod 5 Public String SayHello (student[] students);
7}
9 public class Teacher { 10 ... 11}
public class Student { + Private Teacher Teacher;
//getters and Setters 17 ... 18} |
This method receives a student array containing hundreds or thousands of student, unlike the example above, where student and teacher are now many-to-one, so there is no "circular reference" problem. Suppose all these student teacher are a person. We tried to convert the student array object to a section of XML, as follows:
1 <student> 2 <teacher> 3 ... 4 </teacher> 5 </student> 6 <student> 7 <teacher> 8 ... 9 </teacher> Ten </student> 11 ... |
The problem comes out, see no, each student node has a teacher node below, and when the XML is converted to an student array by the receiver, each student has a teacher of their own, and the teacher objects are copied hundreds of times, After such a transformation-transfer-transformation process, this array object really becomes a "monster".
The root of the problem is that the relationship between student and teacher is many-to-one, and when the "many" side is transmitted, the "one" side may be duplicated multiple times. This consumes a large amount of network transmission bandwidth and memory. Here the parameters are not necessarily a collection or an array, such as Objecta and OBJECTB have a reference to OBJECTC, after soap transmission, the OBJECTC from one to two, respectively belonging to Objecta and OBJECTB, Instead of sharing a OBJECTC anymore.
"Editor's recommendation"
- XML WebService Full Instance detailed parsing
- Getting started with XML creating well-constructed XML
- Three common errors to avoid when implementing XML and Web Services