WebService instances in Java

Source: Internet
Author: User

WEB Services is an online application service published by an enterprise that completes its specific business needs, and other companies or applications can access and use the online service via the Internet.

Key technologies and rules for WEB service:

1.XML: A standard method for describing data.

2.SOAP: The protocol that represents the exchange of information (Simple Object Access Protocol).

The 3.wsdl:web service describes the narrative language.

4.UDDI: General description, Discovery and integration, a platform-independent, XML-based protocol for describing business on the Internet.

One, using the JDK Web Service API implementation, which uses the SOAP message-based Web services:

1. First create a Web services project as a Web services Endpoint.

2. Create a Helloservice.java class

Package Com.yjpeng.hello;import Javax.jws.webservice;import javax.jws.webmethod;import javax.xml.ws.Endpoint;@ Webservicepublic class HelloService {@WebMethodpublic string SayHello (String message) {return "Hello," + Message;} public static void Main (string[] args) {//create and publish an endpointhelloservice hello = new HelloService (); Endpoint Endpoint = endpoint.publish ("Http://localhost:8080/helloService", Hello);}}

3. After using apt to compile the Helloservice.java (for example, Apt-d bin (bin holds the compiled file folder) Scr/com/yjpeng/hello/helloservice.java), a Jaxws folder is generated.

4. Using Java Com.yjpeng.hello.HelloService to run the Helloservice.java file, enter HTTP://LOCALHOST:8080/HELLOSERVICE?WSDL in the browser such as

5. Use the Wsimport command to generate the Client:wsimport-p com.yjpeng.webservice-keep http://localhost:8080/helloService?wsdl The following files are generated in the current folder, for example:

6. Write the client file Helloclient.java

Package Com.yjpeng.hello;import Com.yjpeng.webservice.helloserviceservice;public class Helloclient {public static void Main (string[] args) {Helloserviceservice helloserviceservice = new Helloserviceservice (); Com.yjpeng.webservice.HelloService HelloService = Helloserviceservice.gethelloserviceport (); System.out.println (Helloservice.sayhello ("Hello"));}}

Execution results in console output Hello, hello proof to use JDK Web Service API to achieve Web services success!

Second, using the Xfire framework, I use the MyEclipse integrated Xfire to test, using Xfire development WebService can have three ways:

A. One is generated from the JavaBean.

B. One is generated from a WSDL file.

C. One is to establish oneself webservice.

The detailed implementation process is as follows:

1. Build Webserviceproject with MyEclipse (note: The framework xfire in Web service&j2ee details), folder structure such as the following:

2. Create the Ihello.java interface

Package Com.yjpeng.hello;public Interface Ihello {public string SayHello (String message);}

3. Create Ihelloimpl.java to implement Ihello.java interface

Package Com.yjpeng.hello;public class Ihelloimpl implements Ihello {public string SayHello (String message) {return me Ssage;}}

4. Change the Service.xml file to add the following code

<service>
<!--a name for the service--
<name>HelloService</name>
<!--Service Interface class--
<serviceClass>
Com.yjpeng.hello.IHello
</serviceClass>
<!--service Interface implementation Class--
<implementationClass>
Com.yjpeng.hello.IHelloImpl
</implementationClass>
<!--wsdl Style--
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>

5. Deploy the Web service project to tomcat inside launch Tomcat in the browser address bar input http://localhost:8080/TestWebServices/services/Hello?wsdl appears for example to

6. Then the WSDL behind the expansion HelloService can see

7. Create a Clienthelloclient.java class

Import Java.net.malformedurlexception;import Java.net.url;import Org.codehaus.xfire.xfirefactory;import Org.codehaus.xfire.client.client;import Org.codehaus.xfire.client.xfireproxyfactory;import Org.codehaus.xfire.service.service;import Org.codehaus.xfire.service.binding.objectservicefactory;import Com.yjpeng.hello.ihello;public class Helloclient {public static void main (string[] args) {Service s = new OBJECTSERVICEFAC Tory (). Create (Ihello.class); Xfireproxyfactory XF = new Xfireproxyfactory (Xfirefactory.newinstance (). Getxfire ()); String url= "Http://192.168.122.128:8080/TestWebServices/services/HelloService"; Ihello hello;try {hello = (Ihello) Xf.create (s, url); System.out.println (Hello.sayhello ("Hello")); catch (Malformedurlexception e) {e.printstacktrace ();} try {//This is a remote Web Service that is called. NET write on the Java side. The assumption that the call native writes the address in the URL for the cost function only needs to be the address of the client C = new Client (the new URL ("http:// WWW.WEBXML.COM.CN/WEBSERVICES/QQONLINEWEBSERVICE.ASMX?WSDL ")); object[] o = C.invoke (" Qqcheckonline ", New string[]{" 271751507 "}); SYstem.out.println (o.length);} catch (Exception e) {e.printstacktrace ();}}}

Execute the Helloclient.java class to output Hello 1.

Third, the use of axis1.4 development WebService method

First download the axis1.4 package and the Tomcatserver, and copy the extracted axis1.4 package to the Tomcatserver folder below the WebApps axis folder. Axis supports the deployment and development of three Web service types, namely:

a.dynamic invocation Interface (DII)

B.stubs Way

C.dynamic Proxy Mode

1. Writing dii (Dynamic invocation Interface) mode Web Service

A. Writing a service procedure Helloclient.java

Public class Helloclient{public string GetName (string name) {return "Hello," + Name;}}

B. Copy the Helloclient.java file to Axis_home and rename it to Helloclient.jws.

C. Access link http://localhost:8080/axis/HelloClient.jws?wsdl page displays the WSDL file that axis has generated itself

D. The Clienttesthelloclient.java to write the access service needs to import the corresponding Axis.jar package under the web-inf/lib/folder of the downloaded axis.

Package Com.yjpeng.webservice;import Java.net.url;import javax.xml.namespace.qname;import Org.apache.axis.client.call;import Org.apache.axis.client.service;public class Testhelloclient {public static void Main (string[] args) {try{string endpoint = "Http://localhost:8080/axis/HelloClient.jws"; Service service = new service (); Call call = (call) Service.createcall (); Call.setoperationname (new QName (endpoint, "GetName")); Call.settargetendpointaddress (new URL (endpoint)); String result = (string) call.invoke (new object[]{"Zhang San"}); SYSTEM.OUT.PRINTLN (result);} catch (Exception e) {e.printstacktrace (); }}}

Perform testhelloclient.java on console output hell, Zhang San, test succeeded.

2. Write DYNAMCI proxy mode access Service

A. Write a helloclient.java that deploys a server-side program, using the top-DII method

public class Helloclient{public string GetName (string name) {return "Hello," + Name;}}

B. Writing the Proxy interface Helloclientinterface.java need to extend the Java.rmi.Remote class

Package com.yjpeng.dynamic.proxy;import Java.rmi.remote;import Java.rmi.remoteexception;public interface Helloclientinterface extends Remote {public string getName (String name) throws RemoteException;}

C. Clienttesthelloclient.java for the preparation of access services

Package Com.yjpeng.dynamic.proxy;import Java.net.url;import javax.xml.namespace.qname;import Javax.xml.rpc.service;import Javax.xml.rpc.servicefactory;public class Testhelloclient {public static void main ( String[] args) {try{string wsdlurl = "http://localhost:8080/axis/HelloClient.jws?wsdl"; String Namespaceurl = "Http://localhost:8080/axis/HelloClient.jws"; String serviceName = "Helloclientservice"; String portname = "Helloclient"; Servicefactory servicefactory = Servicefactory.newinstance (); Service Afservice = Servicefactory.createservice (new URL (Wsdlurl), New QName (Namespaceurl, serviceName)); Helloclientinterface proxy = (helloclientinterface) afservice.getport (New QName (Namespaceurl, PortName), Helloclientinterface.class); System.out.println (Proxy.getname ("Zhang San"));} catch (Exception e) {e.printstacktrace (); }}}

Perform testhelloclient.java on console output hell, Zhang San, test succeeded.

Iv. development of WebService using AXIS2

In the study ....

V. Opening a WebService interface instance in a Java Web project

1. Introduction of the required JAR package

2. Create an interface class Iaddnumbers.java

Package Com.yjpeng.webservice;public interface iaddnumbers {public int addnumbers (int a, int b);}

3. Create a Addnumberimpl.java implementation Iaddnumbers.java interface class

Package Com.yjpeng.webservice;import Javax.jws.webservice;import Com.sun.xml.ws.transport.http.servlet.WSServlet, @WebService (targetnamespace= "http://webservice.yjpeng.com", Servicename= "Addnumberimplservice", Portname= "Addnumberimpl") public class Addnumberimpl implements Iaddnumbers { public int AddNumbers (int a, int b) {return a + B;}}

4. Create a Sun-jaxws.xml file under the Wen-inf folder

<?xml version= "1.0"? ><endpoints version= "2.0" xmlns= "Http://java.sun.com/xml/ns/jax-ws/ri/runtime" > <endpoint name= "Addnumberimpl" implementation= "Com.yjpeng.webservice.AddNumberImpl" url-pattern= "/addnum Berimpl "/> </endpoints>

5. In the Web. xml file, add

<servlet><servlet-name>AddNumberService</servlet-name><servlet-class> com.sun.xml.ws.transport.http.servlet.wsservlet</servlet-class><load-on-startup>1</ load-on-startup></servlet><servlet-mapping><servlet-name>addnumberservice</ Servlet-name><url-pattern>/addnumberimpl</url-pattern></servlet-mapping><listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener>

6. Deploy the Web project to launch the corresponding deployment server. Visit HTTP://LOCALHOST:8080/TESTJAXWEBSERVICE/ADDNUMBERIMPL?WSDL This opens the interface for a Web service every time you start webserver.

WebService instances in Java

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.