Current location - Health Preservation Learning Network - Healthy weight loss - What are the ways of java Webservice? Which way is better?
What are the ways of java Webservice? Which way is better?
The application of webservice is more and more extensive. Here are several ways to develop webservice in Java system, which is equivalent to making a record.

1.Axis2

Axis is apache's next open source webservice development component, which appeared earlier and more mature. This paper mainly introduces the development of Web services by Axis+eclipse. Of course, the development and publication of Web services can be done without eclipse, but it will be more convenient to use eclipse.

(1) Download the Java EE version of eclipse.

(2) Download axis2.

(3) Download axis2 plug-in of eclipse.

Axis2_Codegen_Wizard

Axis2 _ Service _ Archiver

The version of 1.3 is recommended.

(4)eclipse installs axis2 plug-ins.

1) Create a new Axis2 folder under any directory, create an eclipse directory under this folder, and create a plugins directory and a features directory under the eclipse directory, for example: d: \ program software \ eclipse-SVN \ Axis2 \ eclipse;

2) Decompress the downloaded axis2 plug-in and put the decompressed file into the newly-built eclipse plug-in directory;

3) Create a new links directory under the %eclipse_home% directory, and create an axis2.link file under the links directory with the following contents: path = d: \ program software \ eclipse-SVN \ Axis2;

4) Restart eclipse and click File-New-Other. If you see the Axis2 wizard, it means that the plug-in was successfully installed.

(5) Install the shaft 2.

(6) Create a new web project with eclipse, and create an ordinary java class with at least one method.

(7) Publishing web services

1) Click eclipse's File-New-Other to open the Axis2 wizard, select the Axis2 service archiver, and then select Next;

2) Select the location of the class file, that is, the storage path of the class file. Note: Select only the class directory, excluding the package folder, and then select Next.

3) Select Skip WSDL, and then select Next.

4) Starting from the next step, select the service XML file to be included in the service archive, and select Automatically generate service XML;

5) Service name-fill in your service name, class name-fill in the class name, including package name, then click Load, and then click Finish, and the webservice is published successfully;

6) Then go to% Tomcat _ home%/webapps/Axis2/Web-INF/services to see if there are any extra ones. Aar document;

Note: the above methods are published to the axis2.war package, and can also be copied and generated. Aar file into your actual application. At the same time, you can use eclipse's create webservice function to publish your webservice, and select axis2 to generate your webservice, so that the webservice will be deployed in your application.

2.Apche CXF

It is also very convenient and simple for CXF to develop webservice, and the integration with spring can be said to be very good. Give an example of CXF developing webservice.

1) Create a new web project in eclipse and import the dependency package, as shown in the following figure:

2) Write an interface, such as:

Note: The parameter of the method in the interface of the webservice developed by CXF must be like this, otherwise the CXF server will not receive the value of the parameter when the client calls. Name: parameter name is optional (it is recommended to write), targetNamespace: namespace must be filled in, which is the reverse order of package name by default, mode: parameter type, and in means input.

3) Write a method to realize the interface of the class;

4) Integrate spring and write a bean file, such as cxf-beans.xml, with the following contents:

Cxf-beans.xml code

& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt

& ltimport resource = " class path:META-INF/cxf/cxf . XML "/& gt;

& ltimport resource = " class path:META-INF/cxf/cxf-extension-soap . XML "/& gt;

& ltimport resource = " class path:META-INF/cxf/cxf-servlet . XML "/& gt;

& ltjaxws:endpoint id = " Vote " implementor = " com . zcl . cxf . service . Vote impl " address = "/Vote "/& gt;

& lt/beans & gt;

This document is easy 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 contents:

Web.xml code

id="WebApp_ID" version="2.5 " >

& lt context parameters & gt

& ltparam-name & gt; contextConfigLocation & lt/param-name & gt;

& lt parameter value & gtweb-INF/cxf-beans.xml < /param-value & gt;

& lt/context-param & gt;

& lt Audience & gt

& lt listener class & gt org.springframework.web.context.contextloader listener < /listener-class & gt;

& lt/listener & gt;

& ltservlet & gt

& ltservlet-name & gt; cxf & lt/servlet-name & gt;

& ltservlet-class & gt; org . Apache . cxf . transport . servlet . cxf servlet & lt; /servlet-class & gt;

& lt Load at startup & gt1< /load-on-startup & gt;

& lt/servlet & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; cxf & lt/servlet-name & gt;

& lturl mode & gt/services/* <; /URL-pattern & gt;

& lt/servlet-mapping & gt;

& lt/we B- app & gt;

Deploy the project to middleware, such as tomcat, and you can access the webservice.

3.JDK develops network services

1) Write a Java class as follows:

Jdkwebservice.java code

Package demonstration;

Import javax.jws.webparam;

Import javax.jws.webservice;

Import javax.xml.ws.endpoint;

@WebService

Public class JdkWebService {

Return "Just do it,"+value+"!" ;

}

Public static void main(String[] args) {

}

}

2) Run the java class to access the webservice on the browser.

Note: this method is not very friendly when developing web projects. We can write a servlet class and publish a webservice in the initialization method of the servlet class, so that our middleware server will help us start automatically when the webservice starts.

4) xfire

There are many frameworks for developing web services, and each framework has its own advantages. Recently, I practiced developing web services with xfire. The following is a small example of developing web services, hoping to help beginners.

1. Create a new java web project named TestWebService, add the jar package related to xfire to the lib directory, and write interface classes and implementation classes.

Java code

Package com.lamp.service;

Common interface message service (

Public string getName (string name);

}

[java] view plaincopyprint?

Package com.lamp.service;

Common interface message service (

Public string getName (string name);

}

Implementation class

Java code

Package com.lamp.service.impl;

Import com.lamp.service.messageservice;

The public class MessageServiceImpl implements MessageService {

Common string getName {

Return to "hello"+name+"and welcome to Web Service World";

}

}

[java] view plaincopyprint?

Package com.lamp.service.impl;

Import com.lamp.service.messageservice;

The public class MessageServiceImpl implements MessageService {

Common string getName {

Return to "hello"+name+"and welcome to Web Service World";

}

}

Create a new folder META-INF under the src directory, then create a new folder xfire under it, and create a new configuration file services.xml under the xfire directory.

Xml code

& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt

& lt service & gt

& lt name & gtMessageService & lt/name & gt;;

& ltserviceClass & gtcom . lamp . service . messageservice & lt; /service class & gt;

& lt implementation class & gt com. lamp.service.impl.messageserviceimpl < /implementation class & gt;

& lt/service & gt;

& lt/beans & gt;

[xml] view plaincopyprint?

& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt

& lt service & gt

& lt name & gtMessageService & lt/name & gt;;

& ltserviceClass & gtcom . lamp . service . messageservice & lt; /service class & gt;

& lt implementation class & gt com. lamp.service.impl.messageserviceimpl < /implementation class & gt;

& lt/service & gt;

& lt/beans & gt;

Finally, configure xfire's servlet in web.xml.

Xml code

& ltservlet & gt

& ltservlet-name & gt; XFireServlet & lt/servlet-name & gt;

& ltservlet-class & gt;

& lt/servlet-class & gt;

& lt/servlet & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; XFireServlet & lt/servlet-name & gt;

& lturl mode & gt/servlet/xfire servlet/* <; /URL-pattern & gt;

& lt/servlet-mapping & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; XFireServlet & lt/servlet-name & gt;

& lturl mode & gt/services/* <; /URL-pattern & gt;

& lt/servlet-mapping & gt;

[xml] view plaincopyprint?

& ltservlet & gt

& ltservlet-name & gt; XFireServlet & lt/servlet-name & gt;

& ltservlet-class & gt;

& lt/servlet-class & gt;

& lt/servlet & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; XFireServlet & lt/servlet-name & gt;

& lturl mode & gt/servlet/xfire servlet/* <; /URL-pattern & gt;

& lt/servlet-mapping & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; XFireServlet & lt/servlet-name & gt;

& lturl mode & gt/services/* <; /URL-pattern & gt;

& lt/servlet-mapping & gt;

So the server-side development is completed, and now the client-side development is started.

Creating a new java project will 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. The code is

Xml code

& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt

& lt project name ="WebService" basedir= ","default="gen-webservice ">

& lt attribute file = "build.properties">

& lt/property & gt;

& ltpath id="project-classpath " >

& lt fileset dir = "$ {lib.dir}">

& ltinclude name="**/*。 jar "/>;

& lt/fileset & gt;

& lt/path & gt;

& lttarget name="gen-webservice " >

& lttaskdef name = " wsgen " class name = " org . code Haus . xfire . gen . wsgentask " class pathref = " project-class path "/& gt;

& ltwsgen output directory = " $ { src . dir } "

wsdl = " $ { wsdl . dir } " package = " com . lamp . ws . client " overwrite = " true "/& gt;

& lt/target & gt;

& lt/project & gt;

[xml] view plaincopyprint?

& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt

& lt project name ="WebService" basedir= ","default="gen-webservice ">

& lt attribute file = "build.properties">

& lt/property & gt;

& ltpath id="project-classpath " >

& lt fileset dir = "$ {lib.dir}">

& ltinclude name="**/*。 jar "/>;

& lt/fileset & gt;

& lt/path & gt;

& lttarget name="gen-webservice " >

& lttaskdef name = " wsgen " class name = " org . code Haus . xfire . gen . wsgentask " class pathref = " project-class path "/& gt;

& ltwsgen output directory = " $ { src . dir } "

wsdl = " $ { wsdl . dir } " package = " com . lamp . ws . client " overwrite = " true "/& gt;

& lt/target & gt;

& lt/project & gt;