Mutual calls between JAVA and. NET-Mutual calls through Web Services

Source: Internet
Author: User

JAVA and. NET are the two most competitive development media in the world today. There are many similarities between the two languages. In many large development projects, two languages are often required for integrated development. Many developers tend to prefer one language, and are afraid of another language when using integrated development. The following describes examples of mutual calls between JAVA and. NET. The following describes how to call each other through common Web services, TCP/IP sockets, and Remote objects.

In this chapter, we will first introduce the simplest and most commonly used methods for mutual calls of Web Services. First, let's talk about the source of Web Services. Web services are a new Web Application Branch that can execute any function from simple requests to complex business processing. After deployment, other Web service applications can discover and call the services deployed by the application. A Web Service is an application that can use standard Internet protocols, such as HTTP, SOAP, and XML, web services are regarded as Web component programming. Web services must provide a standard type system for communicating different types of systems in different platforms, programming languages, and component models.

XML and XSD
The extensible markup language XML is the basic format for data representation on the Web Service platform. In addition to ease of creation and analysis, XML has the primary advantage of being independent from the platform and the vendor. XML was created by the World Wide Web society (W3C). XML SchemaXSD developed by W3C defines a set of standard data types and provides a language to extend this data type. The Web Service platform uses XSD as the data type system. When you construct a Web Service in a language such as JAVA and C #, to comply with the Web Service standard, all the data types you use must be converted to the XSD type. To enable it to be transmitted across different organizations on different platforms and software, you also need to package it through the SOAP protocol.
SOAP
SOAP is the Simple Object Access Protocol. It is a lightweight Protocol used to exchange XML encoding information. It has three main aspects: XML-envelope defines a framework for describing information content and how to handle content, encodes program objects into XML object rules, and executes Remote Procedure Calls (RPC). SOAP can run on any other transmission protocol. For example, you can use SMTP, or the internet email protocol, to transmit SOAP messages, which is tempting. The headers in the transport layer are different, but the XML payload remains the same. The Web Service allows different systems to call each other in the form of "software-software conversation", breaking the incompatibility between software applications, websites, and various devices, achieve the goal of "seamless Web-based integration.
WSDL
Web Service Description Language (WSDL) is a formal description document provided by machines that can be read. It is an XML-based language used to describe Web services and their functions, parameters, and returned values. Because it is based on XML, WSDL is machine readable and human readable.

The following two aspects explain how to implement mutual calls between JAVA and. NET through Web Services.

 


1. Use. NET as the server and JAVA as the client to implement mutual calls.

In. in the. NET system, using WCF as a new generation of service development tools is a new selling point of Microsoft. Let's use WCF as an example to implement the server end. First, create a website project, add a WCF Service PersonService to the website. You will see the PersonService. svc, IPersonService, and PersonService. cs files. IPersonService exposes an interface. The function of the interface is implemented by PersonService, and the client uses PersonalService. svc to find the service and add reference to it.

Code
// In the PersonService. svc contains only one row, which lists the implementation classes of the Service <% @ ServiceHost Language = "C #" Debug = "true" Service = "Service. personService "CodeBehind = "~ /App_Code/PersonService. cs "%> // service implementation using System; using System. collections. generic; using System. linq; using System. runtime. serialization; using System. serviceModel; using System. text; // Note: You can use the "RENAME" command on the "refactoring" menu to change the Interface Name "IPersonService" in the Code and configuration file at the same time ". Namespace Service {[ServiceContract] public interface IPersonService {[OperationContract] IList <Person> GetList ();} public class PersonService: IPersonService {public IList <Person> GetList () {IList <Person> personList = new List <Person> (); Person person1 = new Person (); person1.ID = 0; person1.Age = 27; person1.Name = "Leslie"; personList. add (person1); Person person2 = new Person (); person2.ID = 1; person2.Age = 23; person2.Name = "Rose"; personList. add (person2); Person person3 = new Person (); person3.ID = 2; person3.Age = 29; person3.Name = "Jack"; personList. add (person3); return personList ;}}}
To use Person for remote transmission, we must serialize the Person. In WCF, there are three parts: service contract, data contract, and message contract, data contracts are used to serialize and process data. If you want to learn more about WCF, you can use WCF to implement SOA-oriented service programming.

Code
Using System; using System. collections. generic; using System. linq; using System. web; using System. runtime. serialization; /// <summary> // abstract description of Person /// </summary> namespace Service {[DataContract] public class Person {[DataMember] public int ID {get; set ;}[ DataMember] public string Name {get; set ;}[ DataMember] public int Age {get; set ;}}}
There are multiple serialization methods in the Data contract, including DataContractSerializer, NetDataContractSerializer, XmlServializer, and DataContractJsonSerializer. Only the most common DataContractSerializer is used here, while DataContractJsonSerializer is a popular method nowadays. Especially when developing network projects, Json is used for data communication.

After configuring web. config, you can successfully publish the WCF Service.

Code
<? Xml version = "1.0"?> <Configuration> <system. serviceModel> <behaviors> <serviceBehaviors> <behavior name = "serviceBehavior"> <serviceMetadata httpGetEnabled = "true"/> // set httpGetEnabled to true, enable the client to successfully capture the Service & nb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.