High-speed realization of Jws-webservice and Axis2-webservice

Source: Internet
Author: User
Tags soap wsdl

Before detailing the webservice in both frameworks, let's talk about SOA awareness, a service-oriented architecture.SOAThe main problem to be addressed is to respond to changing business requirements by combining existing applications and infrastructure in the context of an existing infrastructure. SOAWith its loosely coupled nature, it enables businesses to join in a modular approach to new services or to update existing services to address new business needs, and one of the main ways in which they provide services externally is what we are going to introduce today.WebService. for now, the more famousWebServiceThere are roughly four frames ofJWS,Axis,xfireas wellCXF. Today we mainly introduce the first two types.

1. jws-webservice.jws is a WebServiceof the Java language implementation fordeveloping and publishing services , it's a lightweight WS- framework that's easy to implement, and here's a little demo to see how JWS is implemented:

(1) Define the interface and advertise the interface as WebService:

Package Org.zttc.service;import Javax.jws.webparam;import Javax.jws.webresult;import javax.jws.webservice;import javax.jws.soap.SOAPBinding; @WebService @soapbinding (style = SOAPBinding.Style.RPC) public interface Imyservice {@ Webresult (name= "Addresult") public int Add (@WebParam (name= "a") int A, @WebParam (name= "b") int b); @WebResult (name= " Loginuser ") Public User Login (@WebParam (name=" username ") string username, @WebParam (name=" password ") string password);}

(2). Define the interface implementation:

Package Org.zttc.service;import Javax.jws.webparam;import Javax.jws.webresult;import javax.jws.webservice;@ WebService (endpointinterface= "Org.zttc.service.IMyService") public class Myserviceimpl implements Imyservice {@ overridepublic int Add (int a, int b) {System.out.println (A + "+" + B + "=" + (A + b)); return a+b;} @Overridepublic User Login (@WebParam (name = "username") string username, @WebParam (name = "password") string password) {Sy STEM.OUT.PRINTLN (username + "is logging"); User user = new user (), User.setid ("1"); User.setusername (username); User.setpassword (password); return user;}}

(3). Announcement Service:

Package Org.zttc.service;import Javax.xml.ws.endpoint;public class MyServer {public static void main (string[] args) { String address = "Http://localhost:8888/ns"; Endpoint.publish (Address, New Myserviceimpl ());}}

(4). Write test class Code:

Package Org.zttc.service;import Java.net.malformedurlexception;import Java.net.url;import Javax.xml.namespace.qname;import Javax.xml.ws.service;public class TestClient {public static void main (string[] args) { try {//create an Urlurl URL for the access WSDL service address = new URL ("http://localhost:8888/ns?wsdl"),//Specify the service's and details through QName QName sname= new QName (" http://service.zttc.org/"," Myserviceimplservice "); Create service Services service = Service.create (url,sname); Implement Interface Imyservice Ms =service.getport (Imyservice.class); System.out.println (Ms.add (12, 33));} catch (Malformedurlexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

By the above code we can see that at the same time call WebService we still need to know the corresponding Java class (interface), but the case of calling webservice often occur between a different system, in advance can not get the corresponding interface, then we can solve the problem? Very easy, we can use the JDK, using the Export command, the interface described in the WSDL description to generate this interface and implementation class, and then the generated interface and implementation class to apply in our own Java program can be, detailed build command is: wsimport-d e:/my cool plate/ My Project/java project/webservice/01/-keep-verbose http://localhost:6666/ns?wsdl.


2. WebService based on the AXIS2 framework. Axis2 is a heavyweight webservice frame relative to JWs,Accurately said it was aWeb services/soap/wsdlthe engine,it can not only produce and publishWebService, and can generateJavaand other language editionsWebServiceclient and service-side code. And it's becauseAxis2has made a very good package that allows us to useAxis2-wsthe time is very simple, the following through a sample sample to introduce youAxis2the application.

(1).The first thing we need to do is download the Axis.war and publish the war package to our Tomcat WebApps. Launch Tomcat, visit:http://localhost:8080/axis2/, assume that the interface shows a demo sample for example, the following indicates the success of the publication:


(2). Write our Java class. Note that the Java class that we build is not placed in any package, directly under SRC, code such as the following:

public class MyService {public    String SayHello (String name,boolean isman) {        if (Isman) {            return ' HELLO,MR ' +n Ame+ "! Welcome to Webservice ";        } else {            return "Hello,miss" +name+ "! Welcome to Webservice ";}}    }

(3).Place the class class we have built into the%tomcat%\webapps\axis2\web-inf\pojo directory (assuming that there is no directory, create a new one and name it Pojo). After you have done this, start Tomcat and visit Http://localhost:8080/axis2/services/listServices, assuming that the following interface indicates the success of the announcement:


(4). after the announcement, we were able to write a client attempt to invoke the Webservice,client code such as the following:

Package Com.unimas.datacollection.webservices.client;import Org.apache.axis2.addressing.endpointreference;import Org.apache.axis2.client.options;import Org.apache.axis2.rpc.client.rpcserviceclient;import Javax.xml.namespace.qname;public class Client {public static void main (string[] args) throws Exception {//RPC call WEBSE Rvicerpcserviceclient serviceclient = new Rpcserviceclient (); Options options = Serviceclient.getoptions ();// Specifies the urlendpointreference er = new EndpointReference ("Http://localhost:8080/axis2/services/MyService") that calls WebService; O Ptions.setto (er);//Specify the SayHello method's value object[] Opaddargs = new object[] {"Zhang San", False};// Class object specifying the data type of the SayHello method return value class[] classs = new class[] {string.class};//Specifies the SayHello method to invoke and the namespace of the WSDL file, The first parameter represents the namespace of the WSDL file//The TargetNamespace property value of the/element can be seen by visiting HTTP://LOCALHOST:8080/AXIS2/SERVICES/MYSERVICE?WSDL QName QName = new QName ("Http://ws.apache.org/axis2", "SayHello");//Call the SayHello method and output The return value of the method//here is the meaning of three parameters: 1, is the QName object, Represents the name of the method to invoke; 2,webservice, the parameter type is objEct[];3, the return value class object, the parameter type is CLASS[],SYSTEM.OUT.PRINTLN (serviceclient.invokeblocking (QName, OPADDARGS,CLASSS) [0]);}} 

Now that Axis2-ws is published, the client code can be run to output the call results in the console. The above is an introduction to the detailed application of JWs and Axis2, in contrast to these two applications can be found to use JWS more flexible, we can arbitrarily add? services without having to deploy again and again, but the workload we have to do is relatively large. And the use of AXIS2 for the development of WebService is quite simple, the only thing we need to do is to develop a common Java class, and then put in the corresponding directory tomcat can, can say small with small advantages, great advantages.

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.