IThaiのブログ

IT関連の話題やタイに関する様々なことを書いていきます。

jdom2でXMLを作成してSOAPリクエストを送信する

SOAPリクエスト送信では、下記のようなApache Axis等のフレームワークでwsdlからスタブ作成する手順が一般的です。

重厚なフレームワークを使わずに、もっとシンプルに実装してみることを目的にHttpClientでSOAPリクエストを送信するところを実装してみました。

まずはWSDLの解説から。。題材は以前と同じで、
http://public.dejizo.jp/SoapServiceV11.asmx?WSDL です。
上のwsdlで見るところは、<soap:operation>のsoapAction属性、<wsdl:operation>のname属性と、上のほうにある<s:sequence>で囲まれたところです。

<wsdl:operation name="GetDicList">
    <soap:operation soapAction="http://MyDictionary.jp/SOAPServiceV11/GetDicList" style="document"/>
        <wsdl:input>
            <soap:body use="literal"/>
        </wsdl:input>
        <wsdl:output>
            <soap:body use="literal"/>
        </wsdl:output>
    </wsdl:operation>

SOAPActionは"http://MyDictionary.jp/SOAPServiceV11/GetDicList"になり、 wsdl:operationのname属性はメソッド名でGetDicListになります。

SOAPAction属性はリクエストHTTPのヘッダに付加します。

<s:element name="GetDicList">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="AuthTicket" type="s:string"/>
        </s:sequence>
    </s:complexType>
</s:element>

<s:sequence>で囲まれたところはメソッドのパラメータになります。

では実際にXMLを作成します。使用するのはjdom2です。

JDOM

Document document = new Document();
Namespace soapenv = Namespace.getNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
Element rootElement = new Element("Envelope", soapenv);
Element bodyElement = new Element("Body", soapenv);
Element getDicListElement = new Element("GetDicList");
Element authTicketElement = new Element("AuthTicket");
getDicListElement.addContent(authTicketElement.setText(""));
bodyElement.addContent(getDicListElement);
rootElement.addContent(bodyElement);
document.addContent(rootElement);

下のXMLが作成されます。

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetDicList>
            <AuthTicket></AuthTicket>
        </GetDicList>
    </soap:Body>
</soap:Envelope>

このXMLのSOAPメッセージを以前の記事を参考にHttpClientでSOAPリクエストしてみました。

package com.example.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.output.XMLOutputter;

public class SoapHttpClient {
    public static void main(String[] args){
        HttpClient httpClient = new DefaultHttpClient();
        Document document = new Document();
        Namespace soapenv = Namespace.getNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        Element rootElement = new Element("Envelope", soapenv);
        Element bodyElement = new Element("Body", soapenv);
        Element getDicListElement = new Element("GetDicList");
        Element authTicketElement = new Element("AuthTicket");
        getDicListElement.addContent(authTicketElement.setText(""));
        bodyElement.addContent(getDicListElement);
        rootElement.addContent(bodyElement);
        document.addContent(rootElement);
        System.out.print(new XMLOutputter().outputString(document));
        String body = new XMLOutputter().outputString(document);
        HttpPost httppost = new HttpPost("http://public.dejizo.jp/SoapServiceV11.asmx");
        StringEntity stringentity=new StringEntity(body,"UTF-8");
        httppost.setEntity(stringentity);
        httppost.addHeader("Content-Type" , "text/xml");
        httppost.addHeader("SOAPAction","http://MyDictionary.jp/SOAPServiceV11/GetDicList");
        HttpResponse response = null;
        try {
            response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            String strresponse = null;
            if(entity!=null) {
                strresponse = EntityUtils.toString(entity, "UTF-8");
            }
            System.out.println(strresponse.toString());
        } catch (Exception e){
            System.out.println(e.toString());
        }
    }
}

これで、SOAPResponseが返ってきました。

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetDicListResponse xmlns="http://MyDictionary.jp/SOAPServiceV11"><GetDicListResult><DicInfo><DicID>8a68bb8a-16ee-4b51-afaa-74c277bb600a</DicID><FullName>Edict和英辞典</FullName><ShortName>Edict和英辞典</ShortName><Publisher /><Abbrev>EdictJE</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>19553dab-aa51-43bb-9fb7-764153b647ac</DicID><FullName>EJDict英和辞典</FullName><ShortName>EJDict英和辞典</ShortName><Publisher /><Abbrev>EJdict</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>fc6bde1b-220d-4171-abe8-7c040db4629e</DicID><FullName>JMdict独和辞典</FullName><ShortName>JMdict独和辞典</ShortName><Publisher /><Abbrev>JMdictDJ</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>3cb5934a-b1df-48db-8f1a-592e6c8d3d20</DicID><FullName>JMdict仏和辞典</FullName><ShortName>JMdict仏和辞典</ShortName><Publisher /><Abbrev>JMdictFJ</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>ba156c74-638c-4b4c-a545-34632f8cf205</DicID><FullName>JMdict露和辞典</FullName><ShortName>JMdict露和辞典</ShortName><Publisher /><Abbrev>JMdictRJ</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>c188c173-cf9e-4840-a4d2-7b00e9906291</DicID><FullName>JMdict和独辞典</FullName><ShortName>JMdict和独辞典</ShortName><Publisher /><Abbrev>JMdictJD</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>4c2315e2-adf4-4648-b88a-7c102126a0ae</DicID><FullName>JMdict和仏辞典</FullName><ShortName>JMdict和仏辞典</ShortName><Publisher /><Abbrev>JMdictJF</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>c86c5ca7-5c6e-46c5-8c1d-25c4e11a7800</DicID><FullName>JMdict和露辞典</FullName><ShortName>JMdict和露辞典</ShortName><Publisher /><Abbrev>JMdictJR</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>39124c12-6244-4c65-94eb-8b18c20c2898</DicID><FullName>フリー百科辞典 ウィキペディア日本語版</FullName><ShortName>ウィキペディア日本語版</ShortName><Publisher>ウィキメディア財団</Publisher><Abbrev>wpedia</Abbrev><StartItemID>NOTICE</StartItemID><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>992c7c7d-0f1d-4127-a567-fd936a65c64c</DicID><FullName>三省堂 デイリーコンサイス英和辞典(試用版)</FullName><ShortName>デイリーコンサイス英和(試用版)</ShortName><Publisher>三省堂</Publisher><Abbrev>DailyEJL</Abbrev><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>IDIOM</ID><Name>成句</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>一部が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption><SearchOption><Name>完全一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>EXACT</MatchOption></SearchOption><SearchOption><Name>成句検索</Name><ScopeID>IDIOM</ScopeID><MatchOption>EXACT</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>8893ca10-e596-4df3-9393-69b799610f7d</DicID><FullName>三省堂 デイリーコンサイス国語辞典(試用版)</FullName><ShortName>デイリーコンサイス国語(試用版)</ShortName><Publisher>三省堂</Publisher><Abbrev>DailyJJL</Abbrev><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>一部が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption><SearchOption><Name>完全一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>EXACT</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo><DicInfo><DicID>439ddafa-87c7-4090-b486-88cac45747a5</DicID><FullName>三省堂 デイリーコンサイス和英辞典(試用版)</FullName><ShortName>デイリーコンサイス和英(試用版)</ShortName><Publisher>三省堂</Publisher><Abbrev>DailyJEL</Abbrev><ScopeList><Scope><ID>HEADWORD</ID><Name>見出し語</Name></Scope><Scope><ID>ANYWHERE</ID><Name>全文</Name></Scope></ScopeList><SearchOptionList><SearchOption><Name>語頭が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>STARTWITH</MatchOption></SearchOption><SearchOption><Name>語尾が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>ENDWITH</MatchOption></SearchOption><SearchOption><Name>一部が一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption><SearchOption><Name>完全一致</Name><ScopeID>HEADWORD</ScopeID><MatchOption>EXACT</MatchOption></SearchOption><SearchOption><Name>本文も検索</Name><ScopeID>ANYWHERE</ScopeID><MatchOption>CONTAIN</MatchOption></SearchOption></SearchOptionList><DefSearchOptionIndex>0</DefSearchOptionIndex><ItemMapList /></DicInfo></GetDicListResult></GetDicListResponse></soap:Body></soap:Envelope>

この次は、WSDLを詳細に調べてみようかな。

現場で使えるJavaライブラリ

現場で使えるJavaライブラリ

広告を非表示にする