WebServiceの適用はますます広範になりました。以下は、JavaシステムでWebサービスを開発するいくつかの方法を紹介します。これは、レコードの作成に相当します。
1.axis2
Axisは、Apacheの次のオープンソースWebサービス開発コンポーネントです。比較的早くて成熟したように見えました。ここでは、主に軸+Eclipse WebServiceの開発を紹介します。もちろん、EclipseなしでWebサービスを開発および公開できますが、Eclipseを使用する方が便利です。
(1)Eclipse //www.vevb.com/softs/239903.html#downのJava EEバージョンをダウンロードします
(2)Axis2 http://axis.apache.org/axis2/java/core/download.cgiをダウンロードします
(3)EclipseのAxis2プラグインをダウンロードします
axis2_codegen_wizard
axis2_service_archiver
http://axis.apache.org/axis2/java/core/tools/index.html
バージョン1.3を使用することをお勧めします
(4)axis2プラグインをインストールするための日食
1)任意のディレクトリに新しいaxis2フォルダーを作成し、このフォルダーの下に新しいEclipseディレクトリを作成し、新しいプラグインディレクトリを作成し、次のようなEclipseディレクトリにディレクトリを機能させます。
2)ダウンロードされたaxis2プラグインを解凍し、減圧されたファイルを新しく作成した日食のプラグインディレクトリに配置します。
3)%eclipse_home%ディレクトリの下に新しいリンクディレクトリを作成し、linksディレクトリに新しいaxis2.linkファイルを作成します。
4)Eclipseを再起動して、file-new-otherをクリックします。 axis2ウィザードが表示されている場合、プラグインが正常にインストールされていることを意味します。
(5)Axis2をインストールします
axis2の戦争分布をダウンロードして減圧し、axis2.warパッケージを%tomcat_home%/webappsの下に配置し、tomcatを開始し、http:// localhost:port/axis2にアクセスし、axis2が正常にインストールされます。
(6)Eclipseを使用して新しいWebプロジェクトを作成し、少なくとも1つの方法を含む通常のJavaクラスを作成します。
(7)ウェブサービスを公開します
1)EclipseでFile-New-otherをクリックし、Axis2ウィザードを開き、Axis2 Service Archiverを選択してから次に選択します。
2)クラスファイルの場所、つまりクラスファイルストレージパスを選択します。注:クラスディレクトリのみを選択し、パッケージフォルダーを含めないでください。
3)WSDLをスキップしてから次に選択します
4)隣に移動して、サービスアーカイブに含まれるサービスXMLファイルを選択し、自動的にTheservice XMLを生成します。
5)サービス名のサービス名、クラス名のクラス名、パッケージ名を含むクラスの名前、[読み込み]をクリックし、[完了]をクリックします。現時点では、Webサービスは正常に公開されます。
6)その後、%tomcat_home%/webapps/axis2/web-inf/servicesに移動して、追加の.ARファイルがあるかどうかを確認します。
7)http:// localhost:8085/axis2/services/class name?wsdlにアクセスして、生成されたWSDLファイルを表示します。
注:上記の方法は、axis2.warパッケージに公開することです。生成された.AARファイルを実際のアプリケーションにコピーすることもできます。同時に、EclipseのCreate Webサイト機能を使用してWebサイトを公開することもできます。 Axis2を選択してWebサイトを生成して、Webサイトがアプリケーションに展開されるようにします。
2.APCHE CXF
CXF開発Webサービスも比較的便利でシンプルであり、Springとの統合は非常に良いと言えます。 CXF Development Webサービスの例を見てみましょう。
1)EclipseおよびImport依存関係パッケージで新しいWebプロジェクトを作成します。
2)次のようなインターフェイスを書きます。
public string test(@webparam(name = "value"、targetnamespace = "http://service.cxf.zcl.com/"、mode = webparam.mode.in)string value);
注:CXFが開発したWebサービスでは、インターフェイス内のメソッドのパラメーターがこのようにしている必要があります。そうしないと、CXFサーバーはクライアントが呼び出すときにパラメーター値を受信しません。名前:パラメーター名、書き込みせず(書き込みます)、ターゲットネームスペース:名前空間、入力する必要があります。デフォルトはパッケージ名、モード:パラメータータイプ、入力を表します。
3)実装クラスを作成し、インターフェイスメソッドを実装します。
4)Springと統合し、次のコンテンツを含むcxf-beans.xmlなどのBeanファイルを書き込みます。
<?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/spring http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <リソース=" classpath:meta-inf/cxf/cxf.xml "/> <Import resource =" class-inf/cxf/cxf-extions-soap.xml "/> Resource = "classPath:Meta-INF/CXF/CXF-SERVLET.XML"/> <JAXWS:endpoint id = "vote" empliontor = "com.zcl.cxf.service.voteimpl" address = "/bot"/> </beans>
このファイルは理解しやすいので、説明しません。
5)CXFServletを構成します
web.xmlファイルでcxfservletを構成し、次のコンテンツでcxf-beans.xmlファイルをロードします。
<web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml/ns/javaeee" 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_55.xsd "id =" webapp_id "version =" 2.5 "> <context-param> <param-name> contextconfiglocation </param-name> <param-value> web-inf/cxf-beans.xml </param-parm> <リスナークラス> org.springframework.web.context.contextloaderlistener </ristener-class> </ristener> <servlet> <servlet-name> cxf </servlet-name> <servlet-class> org.apache.cxf.transport.transport.servlet.cxfservlet < </servlet> <servlet-mapping> <servlet-name> cxf </servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
Tomcatなどのミドルウェアにプロジェクトを展開すると、Webサイトサービスにアクセスできます。
3.JDK開発Webサービス
1)Javaクラスを次のように書きます。
パッケージデモ; javax.jws.webparamをインポートします。 javax.jws.webserviceをインポートします。 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 it do it、" + value + "!"; } public static void main(string [] args){endpoint.publish( "http:// localhost:8080/jdkwsdemo/demo.jdkwebservice"、new jdkwebservice(); }} 2)Javaクラスを実行すると、ブラウザのWebサービスにアクセスできます。
注:この方法は、Webエンジニアリングを開発するときにそれほどフレンドリーではありません。ミドルウェアサーバーが自動的に開始するのに役立つように、サーブレットクラスの初期化方法でウェブサービスを作成してWebサービスを公開できます。
3)Xfire
Webサービスを開発するための多くのフレームワークがあり、各フレームワークには独自の機能があります。最近、Xfireを使用して、開発の開発を練習しました。以下は、Webサービスの開発の小さな例です。始めている人に役立つことを願っています。
1. TestWebserviceという名前の新しいJava Webプロジェクトを作成し、Xfire関連のJARパッケージをLIBディレクトリに追加し、インターフェイスクラスと実装クラスを書き込みます
パッケージcom.lamp.service; public interface messageservice {public string getName(string name); }パッケージcom.lamp.service; public interface messageservice {public string getName(string name); }実装クラス
パッケージcom.lamp.service.impl; com.lamp.service.messageserviceをインポートします。 public class messageServiceImplはmessageservice {public string getname(string name){return "Hellow" + name + "、Welcome to WebService World"; }}パッケージcom.lamp.service.impl; com.lamp.service.messageserviceをインポートします。 public class messageServiceImplはmessageservice {public string getname(string name){return "Hellow" + name + "、Welcome to WebService World"; }} SRCディレクトリに新しいフォルダーMeta-INFを作成し、その下に新しいフォルダーXfireを作成し、Xfire Directoryで新しい構成ファイルServices.xmlを作成します
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://xfire.codehaus.org/config/1.0"> <services> <name>メッセージ</name> <serviceclass> com.lamp.service.messevice </serviceclass> <emontalingclass> com.lamp.service.impl.messageserviceimpl </dexplientclass> </service> </beans> <?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://xfire.codehaus.org/config/config/1.0 <ServiceClass> com.lamp.service.messageservice </serviceclass> <emontalclass> com.lamp.service.impl.messageserviceimpl </dexplimentclass> </service> </beans>
最後に、web.xmlでXfireのサーブレットを構成します
<Servlet> <Servlet-Name> Xfierservlet </servlet-name> <servlet-class> org.codehaus.xfire.transport.http.xfireconfigurableservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> xfireservlete </servlet-name> <url-pattern>/servlet/xfierservlet/*</url-pattern> </servlet-mapping> <servlet-name> xfierservlet </servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet-name> xfireservleteme> </servlet-name> org.codehaus.transport.http.xfireconfigurableservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> xfierservlet </servlet-name> <url-pattern>/servlet/xfireservlet/* <Servlet-Name> Xfierservlet </servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
プロジェクトが展開された後、http:// localhost:8080/testwebservice/servicesを介してブラウザのリモートアクセスインターフェイスを確認し、http:// localhost:8080/testwebservice/services/messagesivice?wsdlとしてwsdlを取得できます。
このようにして、サーバーの開発が完了し、クライアントの開発が開始されました
新しいJavaプロジェクトを作成し、Xfire関連の瓶を紹介します。 ANTを使用してクライアントにプロキシオブジェクトを生成し、プロジェクトパスの下に新しいbuild.xmlを作成します。コードはです
<?xml version = "1.0" encoding = "utf-8"?> <プロジェクト名= "webservice" beadir = "。"。 "。 default = "gen-webservice"> <Property file = "build.properties"> </property> <path id = "project-classpath"> <fileset dir = "$ {lib.dir}"> <includ = "**/*。Jar"/>> </fileset> </path> <ターゲットname " classname = "org.codehaus.xfire.gen.wsgentask" classpathref = "/> <wsgen outputdirectory =" $ {src.dir} "wsdl =" $ {wsdl.dir} "package =" com.lamp.write "/prict = </</<> <?xml version = "1.0" encoding = "utf-8"?> <プロジェクト名= "webservice" beadir = "。"。 "。 default = "gen-webservice"> <Property file = "build.properties"> </property> <path id = "project-classpath"> <fileset dir = "$ {lib.dir}"> <includ = "**/*。Jar"/>> </fileset> </path> <ターゲットname " classname = "org.codehaus.xfire.gen.wsgentask" classpathref = "/> <wsgen outputdirectory =" $ {src.dir} "wsdl =" $ {wsdl.dir} "package =" com.lamp.write "/prict = </</<>紹介したbuild.propertiesファイルはプロジェクトパスにもあります
src.dir = $ {beadir}/src lib.dir = f:/webservice/xfire-1.2.6/lib wsdl.dir = http:// localhost:8080/testwebservice/services/messageservice?wsdl lib.jarが私のためにXfireの道を保存する場所、アリを実行してプロキシオブジェクトを取得します
テストクラスを書きます
パッケージcom.lamp.test; com.lamp.ws.client.messageserviceClientをインポートします。 com.lamp.ws.client.messageserviceporttypeをインポートします。 public class testgetName {public static void main(string [] args){messageServiceClient MSG = new MessagesErviceClient(); messageServicePortType porttype = msg.getMessagesErviceHtpport();文字列result = porttype.getName( "Zhang San"); system.out.println(result); }}パッケージcom.lamp.test; com.lamp.ws.client.messageserviceClientをインポートします。 com.lamp.ws.client.messageserviceporttypeをインポートします。 public class testgetName {public static void main(string [] args){messageServiceClient MSG = new MessagesErviceClient(); messageServicePortType porttype = msg.getMessagesErviceHtpport();文字列result = porttype.getName( "Zhang San"); system.out.println(result); }}コンソールで走って、私はHellow Zhang San、Webservice Worldへようこそ、そして簡単なWebService開発が完了したのを見ました。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。