A detailed explanation of several ways of Java development WebService _java

Source: Internet
Author: User

The application of WebService has become more and more extensive, and here are some ways to develop webservice in the Java system, which is equivalent to making a record.

1.axis2

Axis is the next open source WebService development component of Apache, it appears relatively early, and more mature. This is mainly about axis+eclipse development webservice, of course, you can also develop and release WebService without Eclipse, but it is easier to use eclipse.

(1) Download the Java EE version of Eclipse http://www.jb51.net/softs/239903.html

(2) Download Axis2 http://axis.apache.org/axis2/java/core/download.cgi

(3) Download Eclipse's Axis2 plugin

Axis2_codegen_wizard
Axis2_service_archiver
Http://axis.apache.org/axis2/java/core/tools/index.html
Recommended use of version 1.3

(4) Eclipse installation Axis2 Plugin

1 Create a new Axis2 folder under any directory, create a new Eclipse directory under that folder, and create a new plugins directory and features directory in the Eclipse directory, for example: D:\programSoftware\eclipse-SVN\ Axis2\eclipse;

2 The download of the Axis2 plugin decompression, and the extracted files into the new Eclipse plugins directory;

3 Create a new links directory in the%eclipse_home% directory, and create a new Axis2.link file in the links directory, which reads: PATH=D:\PROGRAMSOFTWARE\ECLIPSE-SVN\AXIS2;

4 Restart Eclipse, click File-new-other, if you see Axis2 Wizards, the plug-in installation is successful.

(5) Installation Axis2

Download the Axis2 War distribution and unpack, place the Axis2.war pack under%tomcat_home%/webapps, start TOMCAT, visit Http://localhost:port/axis2, AXIS2 installation was successful.

(6) Use Eclipse to create a new Web project, creating a normal Java class that contains at least one method.

(7) Publish WebService

1 Click Eclipse's file-new-other, open Axis2 Wizards, select Axis2 Service archiver, then next;

2 Select class file Location, that is, class files to store the path, note: Select only to the classes directory, do not include the package folder, and then next;

3 Select Skip WSDL, then next

4) All the way next to the Select the service XML file to is included in the service archive, tick generate Theservice XML automatically;

5 Service name-Fill in the name of your service, class name-fill in the category name, to include the package name, then click Load, then click Finish, then webservice on the release of success;

6 then go to%tomcat_home%/webapps/axis2/web-inf/services to see if there is one more. AAR document;

7 access the Http://localhost:8085/axis2/services/class name? wsdl to see the generated WSDL file.
Note: The above method is published in the Axis2.war package, you can also copy the generated. aar file to your application, and you can also use Eclipse's Create WebService feature to publish your WebService, Choose Axis2 to generate your webservice so that WebService will be deployed to your application.

2.Apche CXF

The CXF development WebService is also more convenient and simple, and its integration with spring can be said to be very good. Give me an example of CXF development WebService.

1 Create a new Web project in Eclipse and import a dependency package.

2 Write an interface, such as:

Public String Test (@WebParam (name= "value", targetnamespace = "http://service.cxf.zcl.com/", mode = WebParam.Mode.IN) String value);

Note: CXF developed WebService, the parameters of the method in the interface must be in this way, otherwise the client calls the CXF server will not receive the value of the parameter, name: parameter name, can not write (recommended write), targetnamespace: namespace, Be sure to fill in the order in which the package name is reversed by default, mode: parameter type, in expression input.

3 Write a realization class, implement the method of interface;

4 and spring integration, write a bean file, such as: Cxf-beans.xml, which reads as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=  
"Http://www.springframework.org/schema/beans" 
  xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:jaxws= "Http://cxf.apache.org/jaxws" 
  xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd  
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">  
 
  <import Resource= "Classpath:meta-inf/cxf/cxf.xml"/>  
  <import resource= "classpath:meta-inf/cxf/" Cxf-extension-soap.xml "/>  
  <import resource= classpath:meta-inf/cxf/cxf-servlet.xml"/>  
    
  < Jaxws:endpoint id= "vote" implementor= "Com.zcl.cxf.service.VoteImpl" address= "/vote"/>  
</beans> 

This document is easier to understand than to explain.

5) Configure Cxfservlet

Configure Cxfservlet in the Web.xml file to load the Cxf-beans.xml file, which reads as follows:

<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns : web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <context-param> & Lt;param-name>contextconfiglocation</param-name> <param-value>web-inf/cxf-beans.xml</ param-value> </context-param> <listener> &LT;LISTENER-CLASS&GT;ORG.SPRINGFRAMEWORK.WEB.C Ontext. contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>cxf& Lt;/servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> &L t;load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> Cxf</servlet-name> <url-pattErn>/services/*</url-pattern> </servlet-mapping> </web-app> 
 

Deploy the project to middleware, such as Tomcat, to access the webservice.

3.JDK Development WebService

1 Write a Java class, as follows:

Package demo;  
 
Import Javax.jws.WebParam;  
Import Javax.jws.WebService;  
Import Javax.xml.ws.Endpoint;  
 
@WebService public  
class Jdkwebservice {public  
 
  String dosomething (@WebParam (name= "value", targetnamespace = " http://demo/", mode = WebParam.Mode.IN) String value" {return  
    "Just do it," + Value + "!";  
  }  
    
  public static void Main (string[] args) {  
    endpoint.publish ("Http://localhost:8080/jdkwsdemo/demo. Jdkwebservice ", New Jdkwebservice ());  
  }  
 

2 Run the Java class, you can access the webservice in the browser.

Note: This approach is not very friendly when developing Web projects. We can write a servlet class, publish WebService in the Servlet class initialization method, so our middleware server will help us automatically webservice when it starts.

3) Xfire

Development WebService frame A lot of each frame have their own a bit, recently I use Xfire practice to develop WebService, the following is a small example of the development of WebService, I hope for beginners some small help

1. Create a new Java Web project named TestWebService, add Xfire-related jar packages to the Lib directory, write interface classes and implementation classes

Package com.lamp.service;  
 
Public interface Messageservice {public  
  string GetName (string name);  
} 
Package com.lamp.service; 
 
Public interface Messageservice {public 
  string GetName (string name); 
}

Implementation class

Package Com.lamp.service.impl;  
 
Import Com.lamp.service.MessageService;  
 
public class Messageserviceimpl implements Messageservice {public  
 
  string GetName (string name) {return  
    " Hellow "+ name +", Welcome to WebService World;  
  }  
 
 
Package Com.lamp.service.impl; 
 
Import Com.lamp.service.MessageService; 
 
public class Messageserviceimpl implements Messageservice {public 
 
  string GetName (string name) {return 
    "Hellow" + name + ", Welcome to WebService World"; 
  } 
 
 

Under the SRC directory, create a new folder Meta-inf, and then under it, create a new folder Xfire, and create a new profile in the Xfire directory Services.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= 
"http://xfire.codehaus.org/config/1.0" > 
 <service> 
  <name>MessageService</name> 
  <serviceClass> Com.lamp.service.messageservice</serviceclass> 
  <implementationClass> com.lamp.service.impl.messageserviceimpl</implementationclass> 
 </service> 
</beans> 
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= 
"http://xfire.codehaus.org/config/1.0" > 
 <service> 
  <name>MessageService</name> 
  <serviceClass> Com.lamp.service.messageservice</serviceclass> 
  <implementationClass> com.lamp.service.impl.messageserviceimpl</implementationclass> 
 </service> 
</beans> 

Finally, configure the Xfire servlet in Web.xml

<servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class> Org.codehaus . Xfire.transport.http.XFireConfigurableServlet </servlet-class> </servlet> &LT;SERVLET-MAPPING&G 
    T <servlet-name>XFireServlet</servlet-name> <url-pattern>/servlet/xfireservlet/*</ url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>xfireservlet</servlet -name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet> <se Rvlet-name>xfireservlet</servlet-name> <servlet-class> Org.codehaus.xfire.transport.http.XFireC Onfigurableservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>x Fireservlet</servlet-name> <url-pattern>/servlet/XFireServlet/*</url-pattern> </ Servlet-mapping> <servlet-mapping&Gt <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </ 
 Servlet-mapping>

After the project is deployed, the interface to see remote access is accessed through http://localhost:8080/TestWebService/services in the browser, and the WSDL is http://localhost:8080/ testwebservice/services/messageservice?wsdl

So server-side development is complete, now start client development

New Java project also introduces Xfire-related jars, I use ant to generate proxy objects on the client, create a new build.xml under the project path, and code

 <?xml version= "1.0" encoding= "UTF-8"?> <project "name=" WebService "basedir=". " default= "Gen-webservice" > <property file= "build.properties" > </property> <path id= "project -classpath "> <fileset dir=" ${lib.dir} "> <include name=" **/*.jar "/> </fileset> & lt;/path> <target name= "Gen-webservice" > <taskdef name= "Wsgen" classname= "Org.codehaus.xfire.gen.WsG" Entask "classpathref=" Project-classpath "/> <wsgen outputdirectory=" ${src.dir} "wsdl=" ${wsdl.dir} "pack Age= "Com.lamp.ws.client" overwrite= "true"/> </target> </project> 
 <?xml version= "1.0" encoding= "UTF-8"?> <project name= "WebService" basedir= "default=". "Gen-webservice" 
 
> 
 
  <property file= "build.properties" > 
  </property> 
 
  <path id= "Project-classpath" > 
    <fileset dir= "${lib.dir}" > 
      <include name= "**/*.jar"/>  
    </fileset> 
  </path > 
 
  <target name= "Gen-webservice" > 
    <taskdef name= "Wsgen" Org.codehaus.xfire.gen.WsGenTask "classpathref=" Project-classpath "/> 
 
    <wsgen outputdirectory=" ${ Src.dir} " 
     wsdl=" ${wsdl.dir} "package= com.lamp.ws.client" overwrite= "true"/> 
     
  </target> 
 

The build.properties file that it introduces is also under the project path

SRC.DIR=${BASEDIR}/SRC 
Lib.dir=f:/webservice/xfire-1.2.6/lib 

where Lib.jar for me to store the Xfire path and run ant to get the proxy object

Write a test class

Package com.lamp.test;  
 
Import com.lamp.ws.client.MessageServiceClient;  
Import Com.lamp.ws.client.MessageServicePortType;  
 
public class Testgetname {public  
 
  static void Main (string[] args) {  
    messageserviceclient msg = new MESSAGESERVICEC Lient ();  
    Messageserviceporttype PortType = Msg.getmessageservicehttpport ();  
    String result = Porttype.getname ("John");  
    SYSTEM.OUT.PRINTLN (result);  
  }  
 
Package com.lamp.test; 
 
Import com.lamp.ws.client.MessageServiceClient; 
Import Com.lamp.ws.client.MessageServicePortType; 
 
public class Testgetname {public 
 
  static void Main (string[] args) { 
    messageserviceclient msg = new MESSAGESERVICEC Lient (); 
    Messageserviceporttype PortType = Msg.getmessageservicehttpport (); 
    String result = Porttype.getname ("John"); 
    SYSTEM.OUT.PRINTLN (result); 
  } 
 
 

Run in the console saw Hellow John, welcome to WebService World so a simple WebService development completed

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.