Joke Collection Website - Blessing messages - Java SMS service

Java SMS service

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 the next open source webservice development component of apache, which appeared earlier and more mature. This paper mainly introduces the development of Web services by Axis+eclipse. Of course, Web services can be developed and published without eclipse, but it is more convenient to use eclipse.

(1) Download the Java EE version of eclipse.

② Download axis2.

(3) Download axis2 plug-in of eclipse.

Axis2_Codegen_Wizard

Axis2 _ Service _ Archiver

The version of 1.3 is recommended.

Eclipse installs the axis2 plug-in.

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 a new 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) Use eclipse to build a new web project and create an ordinary java class, including at least one method.

Publish network services

1) Click eclipse 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) Select the service XML file to be included in the service archive from the next step, and select Auto-generate service XML;

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

6) Then go to% Tomcat _ home%/webapps/Axis2/web-INF/services to see if there are any additional. 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 its 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 (writing is recommended), targetNamespace: namespace must be filled in, and the default is the reverse order of package name, mode: parameter type, and in means input.

3) Write an implementation class to realize the method of the interface;

4) Integrate with 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

& lt import resource = "classpath: meta-INF/cxf/cxf.xml"/>

& lt import resource = "classpath: meta-INF/cxf/cxf-extension-soap.xml"/>

& lt import resource = "classpath: meta-INF/cxf/cxf-servlet.xml"/>

& 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 the CXFServlet in the web.xml file and load the cxf-beans.xml file as follows:

Web.xml code

ID =“web app _ ID“version =“2.5“& gt。

& 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 Listener & 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》

& 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 then you can access the webservice.

3.JDK develops network services

1) Write the following Java classes:

Jdkwebservice.java code

Software package demonstration;

Import javax.jws.webparam;

Import javax.jws.webservice;

Import javax.xml.ws.endpoint;

@ Network Service

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 the webservice automatically when it 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 xfire-related jar packages 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 "hellow“+name++",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 "hellow“+name++",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

< name & gt message service

& 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

< name & gt message service

& 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;

This completes the server-side development, and now the client-side development is started.

Creating a new java project also introduces xfire-related jars. I use ant to generate proxy objects on the client and create a new build.xml under the project path. The password is

Xml code

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

& lt project name = "web service" basedir = ","default =“gen-web service“& gt; ">

& lt property file = "build.properties" >

& lt/property & gt;

& ltpath id =“project-class path“& gt;

& lt file set directory = "$ {lib.dir}" >

& lt include name = "* */*. jar“/》;

& lt/fileset & gt;

& lt/path & gt;

& lttarget name =“gen-web service“& gt。

& lttaskdef name =“wsgen“class name =“org . codehaus . 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 = "web service" basedir = ","default =“gen-web service“& gt; ">

& lt property file = "build.properties" >

& lt/property & gt;

& ltpath id =“project-class path“& gt;

& lt file set directory = "$ {lib.dir}" >

& lt include name = "* */*. jar“/》;

& lt/fileset & gt;

& lt/path & gt;

& lttarget name =“gen-web service“& gt。

& lttaskdef name =“wsgen“class name =“org . codehaus . 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;