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.eclipse.org/downloads/
(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, import a dependency package, as shown in figure:
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:
Cxf-beans.xml Code <?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" /> &Nbsp; <import resource= "Classpath:meta-inf/cxf/cxf-servlet.xml" /> <jaxws:endpoint id= "vote" implementor = "Com.zcl.cxf.service.VoteImpl" address= "/vote" /> </beans>
<?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 as follows:
Web.xml code <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> <param-name>contextConfigLocation</param-name> <param-value>web-inf/cxf-beans.xml</param-value > </context-param> <listener> < listener-Class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> < Servlet> <servlet-name>cxf</ Servlet-name> <servlet-class> Org.apache.cxf.transport.servlet.cxfservlet</servlet-class> <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </ Servlet-mapping> &NBSP;&NBSp </web-app>
<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> < Param-name>contextconfiglocation</param-name> <param-value>web-inf/cxf-beans.xml</param-value > </context-param> <listener> <listener-class>
Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> & Lt;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:
Jdkwebservice.java Code 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 ()); } }
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.
4) Xfire Development WebService framework of many, each frame has its own a bit, recently I use Xfire practice to develop WebService, the following is a small example of the development of WebService, I hope that the people who get started 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
Java Code 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
Java code
PackageCom.lamp.service.impl;
ImportCom.lamp.service.MessageService;
Public classMessageserviceimpl
ImplementsMessageservice {
PublicString 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 code
<?xmlVersion= "1.0" encoding= "UTF-8"
?> <beansXmlns= "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
XML code
<servlet> <servlet-name>Xfireservlet
</servlet-name> <servlet-class>Org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class> </servlet> <servlet-mapping> <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> <servlet-name>xfireservlet</servlet-name > <servlet-class> org.codehaus.xfire.transport.http.XFireConfigurableServlet </servlet-class> </ servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/ Servlet/xfireservlet/*</url-pattern> </servlet-mapping>