LoginSignup
4
7

More than 5 years have passed since last update.

Webサービス・クライアントの作成

Last updated at Posted at 2016-05-04

Webサービス・クライアントについて、主に以下の3つのコーディング方法があります。
 - ディスパッチ・クライアント
 - 動的プロキシー・クライアント
 - Spring JaxWsPortProxyFactoryBeanの利用

ディスパッチ・クライアント

ディスパッチベースのWebサービスクライアントの実装例です。

// サービス名
QName serviceName = new QName("sample.test.co.jp:wsdl","SampleService");
// ポート名
QName portName = new QName("sample.test.co.jp:wsdl","SamplePort");
// WSDL URL
URL wsdlLocation = new URL("http://localhost:8080/Sample/SampleImp?wsdl");

// リクエストSOAPMessageインスタンスの作成
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage requestMessage = mf.createMessage();
requestMessage.getSOAPBody().addDocument(requestXmlDoc);
requestMessage.saveChanges();

// Webサービスインスタンスの作成
Service service = Service.create(wsdlLocation, serviceName);
// ディスパッチインスタンスの作成
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
// メッセージの送信
SOAPMessage responseMessage = dispatch.invoke(requestMessage);

動的プロキシー・クライアント

動的プロキシーベースのWebサービスクライアントの実装例です。

// サービス名
QName serviceName = new QName("sample.test.co.jp:wsdl","SampleService");
// ポート名
QName portName = new QName("sample.test.co.jp:wsdl","SamplePort");
// WSDL URL
URL wsdlLocation = new URL("http://localhost:8080/Sample/SampleImp?wsdl");

// Webサービスインスタンスの作成
Service service = Service.create(wsdlLocation, serviceName);
// プロキシインスタンスの作成
SampleService port = service.getPort(portName, SampleService.class);
// メソッドの呼出
OutputBean result = port.execute(inputBean);

Spring JaxWsPortProxyFactoryBeanの利用

Webサービスの動的プロキシを公開するためにSpring JaxWsPortProxyFactoryBeanを利用する実装例です。

<bean class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean" id="sample">
    <property name="serviceInterface" value="jp.co.test.wsdl.Sample />
    <property name="wsdlDocumentUrl" value="http://localhost:8080/Sample/SampleImp?wsdl" />
    <property name="namespaceUri" value="sample.test.co.jp:wsdl" />
    <property name="serviceName" value="SampleService" />
    <property name="portName" value="SamplePort" />
    <property name="lookupServiceOnStartup" value="false" />
</bean>

動的プロキシを実行する。

@Component
public class SendProductInfo {

    @Autowired
    private Sample sample;

    @Override
    public OutputBean execute(InputBean inputBean) {
        // プロキシの実行
        OutputBean result = sample.execute(inputBean);
    }
}
4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7