The application of webservice has become more and more extensive. The following introduces several 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 appeared relatively early and mature. Here we mainly introduce the development of Axis+eclipse webservice. Of course, you can develop and publish webservice without eclipse, but it will be more convenient to use eclipse.
(1) Download the Java EE version of eclipse //www.VeVB.COM/softs/239903.html#down
(2) Download axis2 http://axis.apache.org/axis2/java/core/download.cgi
(3) Download the axis2 plugin of eclipse
Axis2_Codegen_Wizard
Axis2_Service_Archiver
http://axis.apache.org/axis2/java/core/tools/index.html
Recommended to use version 1.3
(4) eclipse to install axis2 plug-in
1) Create a new Axis2 folder in any directory, create a new eclipse directory under this folder, and create a new plugins directory and features directory in the eclipse directory, such as: D:/programSoftware/eclipse-SVN/Axis2/eclipse;
2) Unzip the downloaded axis2 plugin and place the decompressed file in the plugins directory of the newly created eclipse;
3) Create a new links directory under the %eclipse_home% directory, and create a new axis2.link file in the links directory, with the content: path=D:/programSoftware/eclipse-SVN/Axis2;
4) Restart eclipse and click ・file-new-other. If you see Axis2 Wizards, it means that the plug-in is installed successfully.
(5) Install axis2
Download the WAR Distribution of Axis2 and decompress it, place the axis2.war package under %TOMCAT_HOME%/webapps, start tomcat, visit http://localhost:port/axis2, and Axis2 is installed successfully.
(6) Use eclipse to create a new web project and create a normal java class, including at least one method.
(7) Publish a webservice
1) Click File-New-other in eclipse, open Axis2 Wizards, select Axis2 Service Archiver, and then Next;
2) Select Class File Location, that is, the class file storage path. Note: only select the classes directory, do not include the package folder, and then Next;
3) Select Skip WSDL and then Next
4) Go Next to Select the Service XML file to be included in the Service archive, and check Generate theservice xml automatically;
5) Service Name-Fill in your service name, Class Name-Fill in the class name, including the package name, then click load, and then click Finish. At this time, the webservice will be published successfully;
6) Then go to %TOMCAT_HOME%/webapps/axis2/WEB-INF/services to see if there is an additional .aar file;
7) Visit http://localhost:8085/axis2/services/class name?wsdl to see the generated wsdl file.
Note: The above method is to publish it to the axis2.war package. You can also copy the generated .aar file to your actual application. At the same time, you can also use the create website function of eclipse to publish your website. Select axis2 to generate your website, so that the website will be deployed to your application.
2.Apche CXF
CXF development webservice is also relatively convenient and simple, and its integration with spring can be said to be very good. Let’s take an example of CXF development webservice.
1) Create a new web project in eclipse and import dependency packages.
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: In the webservice developed by CXF, the parameters of the methods in the interface must be in this way, otherwise the CXF server will not receive the parameter value when the client calls it. Name: parameter name, do not write it (recommended to write it on), targetNamespace: namespace, must be filled in. The default is the reverse order of the package name, mode: parameter type, IN represents input.
3) Write an implementation class and implement the interface method;
4) Integrate with spring, write a bean file, such as: cxf-beans.xml, with the following content:
<?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 file is easier to understand, so I won't explain it.
5) Configure CXFServlet
Configure CXFServlet in the web.xml file and load the cxf-beans.xml file, with the following content:
<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> </web-app>
Deploy the project to middleware, such as tomcat, and you can access the website service.
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 and you can access the webservice on the browser.
Note: This method is not very friendly when developing web engineering. We can write a servlet class and publish a webservice in the initialization method of the servlet class, so that our middleware server will automatically help us to start.
3) xfire
There are many frameworks for developing WebService, and each framework has its own features. Recently, I used xfire to practice developing WebService. Below is a small example of developing WebService. I hope it will be helpful to those who are getting started.
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"; } } Create a new folder META-INF in the src directory, and then create a new folder xfire under it, and create a new configuration file services.xml in the xfire directory
<?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 xfire's servlet in web.xml
<servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class> org.codehaus.xfire.transport.http.XFireConfigurableServlet </servlet-class> </servlet> <servlet-mapping> <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> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
After the project is deployed, you can see the remote access interface in the browser through http://localhost:8080/TestWebService/services, and get wsdl as http://localhost:8080/TestWebService/services/MessageService?wsdl
In this way, the server development is completed, and now the client development is started
Create a new java project and also introduce xfire-related jars. I use ant to generate proxy objects on the client, and create a new build.xml under the project path, and the code is
<?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" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" /> <wsgen outputDirectory="${src.dir}" wsdl="${wsdl.dir}" package="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" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" /> <wsgen outputDirectory="${src.dir}" wsdl="${wsdl.dir}" package="com.lamp.ws.client" overwrite="true"/> </target> </project> The build.properties file it introduced is also in the project path
src.dir=${basedir}/src lib.dir=F:/WebService/xfire-1.2.6/lib wsdl.dir=http://localhost:8080/TestWebService/services/MessageService?wsdl where lib.jar stores the path of xfire for me, 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 MessageServiceClient(); MessageServicePortType portType = msg.getMessageServiceHttpPort(); String result = portType.getName("Zhang San"); 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 MessageServiceClient(); MessageServicePortType portType = msg.getMessageServiceHttpPort(); String result = portType.getName("Zhang San"); System.out.println(result); } } Running on the console, I saw hellow Zhang San, welcome to WebService world, and a simple WebService development has been completed.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.