WebServiceの概要
WebServiceの概要
最近、会社の業務をする時、いくつかの業者と接続する必要があります.その中のいくつかの業者が提供するインタフェースはWebService技術を使っているので、WebService技術を勉強して、以下のようにまとめました.
一.概要 Webサービスは、リモート・システムからビジネス・データを取得するためのリモート・コール・テクノロジーです.Webサービスは、組織内または複数の組織間のビジネスプロセスの統合に共通のメカニズムを提供し、開放性、プラットフォーム間性を特徴としています.システムとシステムの間でSOAPプロトコルを使用して通信します. SOAP:すなわち、簡単なオブジェクトアクセスプロトコルは、httpで送信されたXML形式のデータを使用して、リモートメソッドを呼び出す目的を達成するものであり、このようにWebサービスはHTTPプロトコルを通じてリモートマシンと対話することができ、また、SOAPはより頑丈で柔軟で使いやすいが、SOAPはWebサービスの専用プロトコルではない.現在SOAPプロトコルは主に2つのバージョンがあり、SOAP 1.1とSOAP 1.2.
二.WebServiceの使用プロセスでは、一般的に、WebServiceを使用するにはサービス側を開発し、サービス側の開発が完了してからパブリッシュに成功すると、サービスの説明ドキュメントが生成されます.説明ドキュメントはWSDLを使用しています(ウェブサービス記述言語)サービス側インタフェース、メソッド、パラメータ、および戻り値に対して、WSDLはサービスの発行に成功したときに自動的に生成され、httpプロトコルのurlを提供してドキュメントにアクセスすることができる.ドキュメントを読むことでクライアントの開発を行うことができ、まずツールを通じてWSDLドキュメントに基づいて基本的なクライアントコードを生成し、次にクライアントロジックを開発してサービス側を呼び出すを選択します.
Java言語を例に、WebServiceのサービス側とクライアントの開発プロセスについて説明します. 1.SEIインタフェースおよびSEIインタフェースを作成する実装クラス 2.≪サービスのパブリッシュ|Publish Services|emdw≫:サービスをパブリッシュする方法は主に次のとおりです. 1.Javaオリジナルメソッド:SEIインタフェースクラスの実装クラスHelloServiceImplに@WebService注記を追加し、mainエントリメソッドで次のコード が表示される. 2.Apache CXFパブリケーション:SEIインタフェースクラスHelloServiceに@WebService注記を追加し、mainエントリメソッドで次のコード 3.Spring統合Apache CXFリリース (1)POM.xmlにSpringとApacheを加える依存 (2)はwebにある.xmlにspringとapache関連servlet を加える(3)アプリケーションContextに次の構成を追加し、Webプロジェクトを開始すると、WebServiceは を正常にリリースしました.
この方法で発行されるサービスのアドレス規則は、次のとおりです.http://ip:ポート番号/プロジェクト名/servletブロッキングパス/サービスパス?wsdlは、この例では(ウェブappsにおけるプロジェクトのパスがウェブサービスであると仮定する)、すなわち、http://127.0.0.1:12345/webservice/ws/hello?wsdl
3.クライアント・コードを生成するには、次の2つの方法があります. 1.jdkが持参するwsimportコマンドを使用して、wsimportコマンドの詳細については公式ドキュメントを参照してください.この例でコードを生成するコマンドは、 です. 2.Apach CXFのwsdl 2 javaコマンドを使用して、詳細は公式文書を参照してください.この例のコマンドは、
4.クライアントコードを記述してサービス呼び出しを行うのは,サービスをパブリッシュする方式に対応し,サービスを呼び出す方式は主に3つある. 1.Javaオリジナルメソッド:サービスクラスHelloServiceImplServiceのインスタンスを作成し、SEIインタフェースの実装クラスインスタンスを取得すると、対応メソッドを呼び出すことができます: 2.Apache CXF呼び出し:JaxWsProxyFactoryBeanのインスタンスを作成し、サービスクラスとサービスアドレスを設定します.このインスタンスによってSEIインタフェースの実装クラスインスタンスを取得すると、対応するメソッド を呼び出すことができます. 3.Spring統合Apache CXF呼び出し (1)まずPOM.xmlにSpringとApacheの依存(同サービス側) を加える(2)アプリケーションContextに以下の設定 を加える.(3)メインメソッドで呼び出す:Springコンテキストコンテナを介してSEIインタフェースのインスタンスを取得し、サービスを直接呼び出す方法 .
上記の3つの呼び出し方式の出力はいずれも「Hello,Sven!」
最近、会社の業務をする時、いくつかの業者と接続する必要があります.その中のいくつかの業者が提供するインタフェースはWebService技術を使っているので、WebService技術を勉強して、以下のようにまとめました.
一.概要
二.WebServiceの使用プロセスでは、一般的に、WebServiceを使用するにはサービス側を開発し、サービス側の開発が完了してからパブリッシュに成功すると、サービスの説明ドキュメントが生成されます.説明ドキュメントはWSDLを使用しています(ウェブサービス記述言語)サービス側インタフェース、メソッド、パラメータ、および戻り値に対して、WSDLはサービスの発行に成功したときに自動的に生成され、httpプロトコルのurlを提供してドキュメントにアクセスすることができる.ドキュメントを読むことでクライアントの開発を行うことができ、まずツールを通じてWSDLドキュメントに基づいて基本的なクライアントコードを生成し、次にクライアントロジックを開発してサービス側を呼び出すを選択します.
Java言語を例に、WebServiceのサービス側とクライアントの開発プロセスについて説明します.
public interface HelloService {
String sayHello(String name);
}
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
String result = "Hello," + name + "!";
System.out.println(result);
return result;
}
}
public class HelloServer {
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:12345/hello", new HelloServiceImpl());
}
}
サービスパブリケーションのアドレスを呼び出します.http://127.0.0.1:12345/hello?wsdlブラウザでアドレスを開くと、サービスのwsdlドキュメント、すなわち、サービスの取扱説明書public class HelloServer {
public static void main(String[] args) {
JaxWsServerFactoryBean serverFactoryBean = new JaxWsServerFactoryBean();
serverFactoryBean.setAddress("http://127.0.0.1:12345/hello");
serverFactoryBean.setServiceClass(HelloService.class);
serverFactoryBean.setServiceBean(new HelloServiceImpl());
serverFactoryBean.create();
}
}
サービスパブリケーションのアドレスを呼び出します.http://127.0.0.1:12345/hello?wsdl <dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>3.1.10version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-httpartifactId>
<version>3.1.10version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-http-jettyartifactId>
<version>3.1.10version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>CXFservlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>CXFservlet-name>
<url-pattern>/ws/*url-pattern>
servlet-mapping>
<jaxws:server address="/hello" serviceClass="com.qipeng.service.HelloService">
<jaxws:serviceBean>
<ref bean="helloService"/>
jaxws:serviceBean>
jaxws:server>
<bean name="helloService" class="com.qipeng.service.impl.HelloServiceImpl"/>
この方法で発行されるサービスのアドレス規則は、次のとおりです.http://ip:ポート番号/プロジェクト名/servletブロッキングパス/サービスパス?wsdlは、この例では(ウェブappsにおけるプロジェクトのパスがウェブサービスであると仮定する)、すなわち、http://127.0.0.1:12345/webservice/ws/hello?wsdl
wsimport -keep -p com.demo.client http://localhost:8080/HelloService?wsdl
wsdl2java -p com.demo.client http://localhost:8080/HelloService?wsdl
で生成されたクライアントコードにHelloServiceImplが含まれています.java、HelloServiceImplService.java、ObjectFactory.java、package-info.java、SayHello.java、SayHelloResponse.JAvaは,パブリケーション方式やコマンド生成のコードによって若干異なるが,概ね以上のクラスである.public class HelloClient {
public static void main(String[] args) {
HelloServiceImplService service =new HelloServiceImplService();
HelloServiceImpl helloService = service.getPort(HelloServiceImpl.class);
String result = helloService.sayHello("Sven");
System.out.println(result);
}
}
public class CXFHelloClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean proxyFactoryBean = new JaxWsProxyFactoryBean();
proxyFactoryBean.setServiceClass(HelloService.class);
proxyFactoryBean.setAddress("http://127.0.0.1:12345/hello");
HelloService service = proxyFactoryBean.create(HelloService.class);
String result = service.sayHello("Sven");
System.out.println(result);
}
}
<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">
<jaxws:client id="helloClient" address="http://127.0.0.1:8080/ws/hello" serviceClass="com.qipeng.hello.client.HelloService"/>
beans>
public class HelloClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloService helloService = (HelloService) context.getBean("helloClient");
String result = helloService.sayHello("Sven");
System.out.println(result);
}
}
上記の3つの呼び出し方式の出力はいずれも「Hello,Sven!」