Developing Java Web Services using SOAP

Source: Internet
Author: User
Tags copy soap web services xmlns java web tomcat log4j wsdl
Web|web Service The book readers should first understand the Java EE, familiar with the basic content such as XML,TOMCAT, this article is a brief introduction to the basic content of Web services, how to build a SOAP service in Java Web development:
First, SOAP (Simple Object access Protocol) Simply objects Access Protocol, to understand soap, you need to understand the origin of distributed computing, with the advent of the next generation of distributed computing system Web services, Soap becomes the actual communication standard for creating and invoking applications that are published over the network. Soap is similar to the traditional binary protocol Iiop (CORBA) and JRMP (RMI), but it does not use binary data notation, but rather uses XML, text-based data representation.
With XML notation, SOAP defines a small wired connection protocol and encoding format to represent data types, programming languages, and databases, as well as the use of various Internet standard protocols as their messaging tools, as well as providing conventions for communication models that represent RPC and document-driven message exchange. Note that the consortium is working on SOAP research, http://www.w3c.org/2000/xp/Group/, and a positive response from mainstream vendors to agree on important tasks related to xml-based protocols and define their key requirements and usage scenarios.
The basic specification of SOAP1.2 defines the following basic elements:
1 syntax and semantics for representing XML documents as structured SOAP messages
2 coding standard for representing data in SOAP messages
3 communication model for exchanging SOAP messages
4 binding of the underlying protocols such as SOAP transmission
The SOAP message mainly includes the envelope header, the message header, the main body, the attachment several points
A simple SOAP message says:
Post/studentinfo http/1.1
Host:anthropology.cun.edu
content-type:text/xml;charset= "Utf-8"
content-length:640
SOAPAction: "Getstudentinfo"

<soap-env:envelop xmlns:soap-env= "Http://www.w3c.org/2001/06/soap-envelope"
Xmlns:xsi= "Http://www.w3c.org/2001/XMLSchema-instance"
Xmlns:xsd= "Http://www.w3c.org/2001/XMLSchema"
soap-env:encodingstyle= "Http://www.w3c.org/2001/06/soap-encoding" >
<SOAP-ENV:Header>
<person:mail xmlns:person= "Http://www.cun.edu/Header" >xyz@cun.edu
</SOAP-ENV:Header>

<SOAP-ENV:Body>
<m:getstudentinfo xmlns:m= "Http://www.cun.edu/jws.student.studentInfo" >
<student_name xsi:type= ' xsd:string ' >
Wang Wen Yin
</student>
</m:GetStudentInfo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelop>
These are the 1.2-version namespaces, the 1.1 namespace soap envelope:http://schemas.xmlsoap.org/soap/envelop/, the soap encoding:http:// schemas.xmlsoap.org/soap/encoding/
For SOAP encoding specifications see the encoded values defined by www.w3c.org/TR/xmlschema-2/, and some other specifications can be viewed on www.w3c.org.
Two, the following from the actual example to learn, here I am using a sub-project of Apache axis of a specific example, to facilitate in-depth understanding of the operation of soap:
1) Download axis related content http://ws.apache.org/axis/:
2 Establish an instance program (comply with Java EE Web Program specification), such as (WebServiceTest directory)
Copy the contents of Axis Lib folder to your webservicetest/web-inf/lib and download xerces on the internet (download location: http://xml.apache.org/xerces-j/ The interpreter's package file Xerces.jar, also to the Webservicetest/web-inf/lib folder, (To configure log4j, please copy the property file Log4j.properties to Webservicetest/web-inf Under/classes folder)
3 Modify the Web.xml file in the application Webservicetest/web-inf: The main servlet is set as follows
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>


<servlet>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>
Org.apache.axis.transport.http.AdminServlet
</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>

<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>

<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
You can enter Http://localhost/WebServiceTest/servlet/TestServlet on the Web site now, see? Axis uses the Org.apache.axis.transport.http.AxisServlet in the Axis.jar package to process the application, which is the basic configuration.
And then we'll say the kernel in axis.
1 do not use the Tomcat engine to run axis.
First set up a footstep file, the environment variable CLASSPATH to set the path of those package files under Lib to include all, run: Java org.apache.axis.transport.http.SimpleAxisServer < Port>
2 The internal service handler is Org.apache.axis.providers.java.RPCProvider, marking the method required by the service, and then providing the parameters from the SOAP request message component.
3 axis's application-side management capabilities:
The Java org.apache.axis.client.AdminClient will list the parameters that you can choose from. Our example is: Java org.apache.axis.client.adminclient-l http://localhost/WebserviceTest/servlet/TestServlet List The list of services is displayed, and the XML file is returned.
4 The Wsdl2java application can create a WSDL file based on Java programs, such as placeholder programs, etc.
Java Org.apache.axis.wsdl.WSDL2java <url>
The basic content of axis is here.
Iv. Specific examples
1 Write logic program, simple as: Soaptest.java
public class soaptest{
public string Getstr (string name) {
Return "Hello," +name;
}
}
2 Deployment Services, writing WSDD file SOAPTEST_DEPLOY.WSDD:
<deployment name= "Simapletest" xmlns= "http://xml.apache.org/axis/wsdd/"
Xmlns:java= "Http://xml.apache.org/axis/wsdd/providers/java"
Xmlns:xsd= "Http://www.w3.org/2000/10/XMLSchema"
xmlns:xsi= "Http://www.w3.org/2000/10/XMLSchema-instance" >
<service name= "Soaptest" provider= "Java:rpc" >
<parameter name= "ClassName" value= "Soaptest"/>
<parameter name= "Allowedmethods" value= "Getstr"/>
</service>
</deployment>
Where the classname parameter is your class name (full name) that you want to deploy, Allowedmethods is the method of the service that is invoked, if there are multiple methods can be separated by a space (for example: <parameter name= "Allowedmethods" value= "Getstr Getmoney"/>), when using * to express all.
All right, now you're ready to deploy, make sure the environment path Classpath set correctly and run:
Java org.apache.axis.client.adminclient-l Http://localhost/WebserviceTest/servlet/TestServlet Soaptest_ Deploy.wsdd
(If you don't understand this, please refer to the above instructions)
OK, hehe, so far, we have completed the deployment of a Web service: test http://localhost/WebServiceTest/servlet/TestServlet See if there is a choice of soaptest service?
If you do not want to serve that rewrite a WSDD file, the content should read:
<deployment name= "Simapletest" xmlns= "http://xml.apache.org/axis/wsdd/"
Xmlns:java= "Http://xml.apache.org/axis/wsdd/providers/java"
Xmlns:xsd= "Http://www.w3.org/2000/10/XMLSchema"
xmlns:xsi= "Http://www.w3.org/2000/10/XMLSchema-instance" >
<service name= "Soaptest"/>
</deployment>
The same as above, the comparison is OK.
V. Client testing:
We can also use Java to test the client, online also have information, you can go to learn, very simple. Now in order to embody the charm of Web services, I use. NET platform to test it, the client uses C # to write (Install the. NET Framework SDK first):
1 generating Web service proxies through WSDL, running under the net platform:
Wsdl/l:cs/protocol:soap/out:soaptestclient.cs http://localhost/WebserviceTest/services/SoapTest?wsdl
We get a CS file SoapTestClient.cs (current directory) via WSDL, you can open the CS file, look at the code inside, that Getstr (string name) is the method we need to call, By calling this method, our client can invoke the server-side method, and the internal transformation Wsdl.exe tool has done it for us, and the Wsdl2java tool under axis has the same function, referring to what I said above about Axis's kernel content
2 Compile CS file into assembly DLL:
Csc/target:library/r:system.web.services.dll/r:system.xml.dll SoapTestClient.cs
Finally we waited for a DLL file SoapTestClient.dll, which the client program would do by calling it.
3 Write client application SoapTestClientApp.cs
Using System;

Namespache jws.client{
public class soaptestclientapp{
Public Soaptestclientapp () {
}
public static void Main (string[] args) {
if (args. Length!=1) {
Console.WriteLine ("Usage:soaptestclientapp <name>");
Environment.exit (1);
}
Soaptestservice st_service=new Soaptestservice ();
St_service.getstr ("Wang wenyin");
}
}
}
4) Compile file Csc/r:soaptestclient.dll SoapTestClientApp.cs
Run Soaptestclientapp
Output results:
Hello,wang Wenyin
Consistent with expected results.

OK, about SOAP development Web services here, I hope this article can be helpful to everyone, thank you: If you have questions, you can leave a message, you can contact me gdanthrowwy@126.com



Related Article

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.