Fast realization of Jws-webservice and Axis2-webservice

Source: Internet
Author: User

Before specifying the webservice of these two frameworks, let's talk about SOA awareness, which is service-oriented architecture.SOAThe main problem to be addressed is to respond to changing business requirements by re-combining existing applications and infrastructure, in the context of an existing infrastructure. SOAWith its loosely coupled nature, it enables enterprises to add new services or update existing services in a modular way to address new business needs, and one of the main ways in which they can provide services externally is what we are going to introduce today.WebService. at present, 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 WebServicefor the Java language implementation todevelop and publish services , it's a lightweight WS- framework that's very simple to implement, and here's a little demo to look at JWS How it's implemented:

(1) Define the interface and publish 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). Publish 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 the 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 a Urlurl URL to access the WSDL service address = new URL ("http://localhost:8888/ns?wsdl"),//Specify the service's and specific information by 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 ();}}}

Through the above code we can see in the call WebService while we still need to know the corresponding Java class (interface), but the situation is often called WebService between a different system, in advance can not get the corresponding interface, then we are how to solve this problem? Very simply, we can use the JDK, using the Export command, the interface description in the WSDL to generate this interface and implementation class, and then the generated interface and implementation class will be applied in our own Java program, the specific 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 you can generateJavaand other language editionsWebServiceclient and service-side code. It is also becauseAxis2we have made a very good package that allows us to useAxis2-wsis very simple, below a small example to introduce youAxis2the application.

(1).The first thing we need to do is download the Axis.war and post the war package to our Tomcat WebApps. Launch Tomcat, Access:http://localhost:8080/axis2/, if the interface displays as follows, the publication is successful:


(2). Write our Java class. Note that the Java class that we created is not placed in any package, it is placed directly under SRC, and the code is as follows:

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 that we have built into the%tomcat%\webapps\axis2\web-inf\pojo folder (if you do not have the folder, create a new one and name it Pojo). After this step, start Tomcat, Access http://localhost:8080/axis2/services/listServices, and if the following interface appears, the publication succeeds:


(4). after the release we can write the client to try to call WebService, the client code is as follows:

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 parameter value of the SayHello method 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 accessing HTTP://LOCALHOST:8080/AXIS2/SERVICES/MYSERVICE?WSDL QName QName = new QName ("Http://ws.apache.org/axis2", "SayHello");//Call the SayHello method and output the method's return value//Here are three parameters meaning: 1, is the QName object, Represents the name of the method to invoke; The parameter value of 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]);}} 

at this point, the AXIS2-WS release completes and the client code executes to invoke the results in the console output. The above is a brief introduction to the specific application of JWS and AXIS2, comparing these two applications can be found to use JWS more flexible, we can arbitrarily increase the service without the need to re-deploy again and again, but we have to do the workload 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 into the corresponding folder tomcat, you can say small with small benefits, 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.