RPC アダプター・ライブラリーのフィーチャー

JSON/XML への結果のマッピング

RPC アダプターでは、出力フォーマットは JSON または XML のどちらかです。

XML への結果のマッピング

以下に、各種シナリオのもとで生成されるさまざまな XML 出力をリストします。

戻りの型が void の場合

戻りの型が void の場合、以下のように、生成される XML 出力は空の結果タグになります。

<results/>

戻りの型が primitive、wrapper、または string の場合

JavaBeans のメソッドが public int getSalary() の場合は、出力は以下のようになります。

<results>80000</results>
JavaBeans のメソッドが public String getMessage() の場合、出力例は以下のようになります。 <results>Hello World</results>
JavaBeans のメソッドが public Boolean isLeapYear(int year) の場合、出力例は以下のようになります。 <results>true</results>

戻りの型が Collection の場合

戻りの型が collection の場合、出力はエレメントの集合であり、各エレメントがコレクションのエントリーを表しています。 コレクションに、抑制されたオブジェクト・タイプのインスタンスが含まれている場合、そのエントリーは無視されます。


JavaBeans のメソッドが public Collection getEmployees() であり、 返された collection に Employee のインスタンスが含まれている場合、出力例は以下のようになります。 <results> <Employee> <firstName>James</firstName> <lastName>Smith</lastName> <designation>Software Developer</designation> <company>IBM</company> </Employee> <Employee> <firstName>John</firstName> <lastName>Dow</lastName> <designation>Manager</designation> <company>IBM</company> </Employee> </results> ユーザーはオブジェクト・タイプの別名を指定できます。 Employee クラスの別名が employee の場合、出力例は以下のようになります。 <results> <employee> <firstName>James</firstName> <lastName>Smith</lastName> <designation>Software Developer</designation> <company>IBM</company> </employee> <employee> <firstName>John</firstName> <lastName>Dow</lastName> <designation>Manager</designation> <company>IBM</company> </employee> </results>

戻りの型が Array の場合

戻りの型が Array の場合、出力はエレメントの集合であり、各エレメントが配列のエントリーを表しています。


JavaBeans のメソッドが public Employee[] getEmployees() であり、 返された Array が Employee のインスタンスから構成されている場合、出力例は以下のようになります。 <results> <Employee> <firstName>James</firstName> <lastName>Smith</lastName> <designation>Software Developer</designation> <company>IBM</company> </Employee> <Employee> <firstName>John</firstName> <lastName>Dow</lastName> <designation>Manager</designation> <company>IBM</company> </Employee> </results>

戻りの型が Map の場合

戻りの型が Map の場合、出力はエレメントの集合になり、各エレメントはマップ内のキー値の組を表します。 ノード名がキーになります。
JavaBeans のメソッドが public Map getDepartments() であり、 返された map が部門コードと部門詳細のキー値の組である場合、 出力例は以下のようになります。

<results> <CS> <deptName>Computer Science</deptName> <deptHead>Dan Johns</deptName> </CS> <EC> <deptName>Electronics and Communication</deptName> <deptHead>Iva Brown</deptName> </EC> </results>

戻りの型が JavaBeans の場合

戻りの型が JavaBeans の場合、すべての読み取りメソッドと読み取りメソッドがない public フィールドは、XML シリアライズとみなされます。JavaBeans はエレメントで表されます。 このエレメントのノード名は、エレメントが表す JavaBeans の型です。 Bean に別名が指定されている場合は、その別名はノード名としても使用されます。
JavaBeans のメソッドが public Employee getEmployee() の場合、出力例は以下のようになります。

<results> <Employee> <firstName>James</firstName> <lastName>Smith</lastName> <designation>Software Developer</designation> <company>IBM</company> </Employee> <results> Employee にマネージャーの Employee 参照が含まれている場合、出力例は以下のようになります。 <results> <Employee> <firstName>James</firstName> <lastName>Smith</lastName> <designation>Software Developer</designation> <company>IBM</company> <manager> <firstName>John</firstName> <lastName>Dow</lastName> <designation>Manager</designation> <company>IBM</company> </manager> </Employee> <results> 助言: employee に collection のフィールド・アドレス・タイプが含まれている場合、出力例は以下のようになります。 <results> <Employee> <firstName>James</firstName> <lastName>Smith</lastName> <designation>Software Developer</designation> <company>IBM</company> <manager> <firstName>John</firstName> <lastName>Dow</lastName> <designation>Manager</designation> <company>IBM</company> </manager> <addresses/> <addresses/> </Employee> <results>

JSON への結果のマッピング

以下に、各種シナリオで生成されるさまざまな JSON 出力について説明します。

戻りの型が void の場合

戻りの型が void の場合、生成される JSON 出力は JSON の結果オブジェクトになります。

{"result":null,"error":null,"id":1}

戻りの型が primitive、wrapper、または string の場合


JavaBeans のメソッドが public int getSalary() の場合、出力例は以下のようになります。 {"result":20000,"error":null,"id":1}
JavaBeans のメソッドが public String getMessage() の場合、出力例は以下のようになります。 {"result":"Hello World","error":null,"id":1}
JavaBeans のメソッドが public Boolean isLeapYear(int year) の場合、出力例は以下のようになります。 {"result":true,"error":null,"id":1}

戻りの型が Collection の場合

戻りの型が collection の場合、出力はエレメントの集合であり、各エレメントがコレクションのエントリーを表しています。 コレクションに、抑制されたオブジェクト・タイプのインスタンスが含まれている場合、そのエントリーは無視されます。


JavaBeans 内のメソッドが public Collection getEmployees() であり、 返されたコレクションに Employee のインスタンスが含まれている場合、出力例は以下のようになります。 {"result":[{"firstName":"James","lastName":"Smith","designation":"Software Developer","company":"IBM"},{"firstName":"John","lastName":"Dow","designation":"CEO","company":"IBM"}],"error":null,"id":1}

戻りの型が Array の場合

戻りの型が Array の場合、出力はエレメントの集合であり、各エレメントが配列のエントリーを表しています。


JavaBeans のメソッドが public Employee[] getEmployees() であり、 返された Array が Employee のインスタンスから構成されている場合、出力例は以下のようになります。 {"result":[{"firstName":"James","lastName":"Smith","designation":"Software Developer","company":"IBM"},{"firstName":"John","lastName":"Dow","designation":"CEO","company":"IBM"}],"error":null,"id":1}

戻りの型が Map の場合

戻りの型が Map の場合、出力はキー値の組の集合になります。
JavaBeans のメソッドが public Map getDepartments() であり、 返された map が部門コードと部門詳細のキー値の組である場合、出力例は以下のようになります。

{"result":[{"deptName":"name1","deptHead":"head1"},{"deptName":"name2","deptHead":"head2"}],"error":null,"id":1}

戻りの型が JavaBeans の場合

JavaBeans は、フィールド名としての key とフィールドの値としての value を持つキー値の組として表されます。public フィールドと getter があるフィールドのみがシリアライズされます。
JavaBeans のメソッドが public Employee getEmployee() の場合、出力例は以下のようになります。

{"result":{"firstName":"James","lastName":"Smith","designation":"Software Developer","company":"IBM"},"error":null,"id":1} Employee にマネージャーの Employee 参照が含まれている場合、出力例は以下のようになります。 {"result":{"firstName":"James","lastName":"Smith","designation":"Software Developer","company":"IBM","manager":{"firstName":"Martin","lastName":"Smith","designation":"Manager","company":"IBM"}},"error":null,"id":1} employee に collection のフィールド・アドレス・タイプが含まれている場合、出力例は以下のようになります。 {"result":{"firstName":"James","lastName":"Smith","designation":"Software Developer","company":"IBM","adresses":["address1","address2","address3"]},"error":null,"id":1}

インスタンスの再利用

RPC アダプターは、(1) 要求別に新規 JavaBeans をインスタンス化する、または (2) ユーザー別にインスタンスを再利用するように構成することができます。(例えば、ショッピング・カート・オブジェクトは後者として構成することができます。) デフォルトでは、要求別に新規 JavaBeans がインスタンス化されます。 再利用は Bean 記述子情報を介して構成します。 詳しくは、SampleBeanInfo API の資料を参照してください。

ベスト・プラクティス

含まれているロジック

一部のコマンドは、サービスとして直接公開しないことを想定して作成されています。 この場合、JavaBeans アクセサーに暗黙のロジックを含めることで作成することができます。 例えば、PlantsByWebSphere サンプルの ShoppingCart EJB には、 addItem(StoreItem item) メソッドが含まれています。StoreItem オブジェクトにはアイテム価格が含まれています。 したがって、この設計では、ShoppingServlet サーブレットの以下のようなものなど、信頼されたソースのみがメソッドを呼び出すと想定しています。

// Add item to cart. if (shoppingCart != null) { String invID = req.getParameter("itemID"); try { Catalog catalog = catalogHome.create(); StoreItem si = catalog.getItem(invID); si.setQuantity(Integer.parseInt(req.getParameter("qty").trim())); shoppingCart.addItem(si); } catch (javax.ejb.CreateException e) { } ShoppingServlet には、addItem(String itemID, String qty) のような仮想メソッドを効率よく作成するロジックが含まれていることに注意してください。その同じロジックは、 以下のように JavaBeans アクセサーに実装することができます。 public class ShoppingCartAccessor implements SelfBeanInfo { private static ShoppingCartHome _home = (ShoppingCartHome) Util.getEJBHome("java:comp/env/ejb/ShoppingCart", com.ibm.websphere.samples.plantsbywebsphereejb.ShoppingCartHome.class); private ShoppingCart _bean; public static String[][] getBeanDescriptorInfo() { String [][] descriptor = { {"bean", "storeInSession", "true"}, {"method", "addItem", "Add item to the shopping cart.", "POST", "itemId", "ID of the desired item", "quantity", "number of items"}, }; return(descriptor); } public ShoppingCartAccessor() throws RemoteException, CreateException { _bean = _home.create(); } public void addItem(String invID, int quantity) throws RemoteException { CatalogAccessor catalog; try { catalog = new CatalogAccessor(); } catch (CreateException e) { throw new RemoteException(e.getMessage(), e); } StoreItem si = catalog.getItem(invID); si.setQuantity(quantity); _bean.addItem(si); } }

XML スキーマ:

以下に、RPCAdapterConfig.xml ファイルの XML スキーマの例を示します。 <xsd:schema xmlns="http://www.ibm.com/xmlns/prod/websphere/featurepack/v6.1/RpcAdapterConfig" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com/xmlns/prod/websphere/featurepack/v6.1/RpcAdapterConfig" elementFormDefault="qualified"> <xsd:element name="services"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="unbounded" ref="POJO"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="description" type="xsd:string"/> <xsd:element name="scope"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Application" /> <xsd:enumeration value="Session" /> <xsd:enumeration value="Request" /> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="validation-regex" type="xsd:string"/> <xsd:element name="validator-ref" type="xsd:string"/> <xsd:element name="default-format" type="xsd:string"/> <xsd:element name="filtered" type="xsd:string"/> <xsd:element name="recursive-object-support" type="xsd:string"/> <xsd:element name="validator"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="1" ref="validation-regex"/> <xsd:element minOccurs="1" ref="validation-class"/> </xsd:sequence> <xsd:attribute name="id" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> <xsd:element name="bean-class" type="xsd:string"/> <xsd:element name="converter-class" type="xsd:string"/> <xsd:element name="converter"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="1" ref="bean-class"/> <xsd:element minOccurs="1" ref="converter-class"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="implementation" type="xsd:string"/> <xsd:element name="methods"> <xsd:complexType> <xsd:sequence> <xsd:element ref="method" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute name="filter" type="xsd:string"/> </xsd:complexType> </xsd:element> <xsd:element name="name" type="xsd:string"/> <xsd:element name="parameters"> <xsd:complexType> <xsd:sequence> <xsd:element ref="parameter" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="rpcAdapter"> <xsd:complexType> <xsd:sequence> <xsd:element ref="default-format" minOccurs="0"/> <xsd:element minOccurs="0" ref="filtered" maxOccurs="1" /> <xsd:element minOccurs="0" ref="recursive-object-support" maxOccurs="1" /> <xsd:element minOccurs="0" ref="converters" maxOccurs="1" /> <xsd:element minOccurs="0" ref="validators" maxOccurs="1" /> <xsd:element minOccurs="1" ref="services" maxOccurs="1"/> <xsd:element minOccurs="0" ref="serialized-params" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="parameter"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="1" ref="name"/> <xsd:element minOccurs="0" ref="description"/> <xsd:element minOccurs="0" ref="validator-ref"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="http-method" type="xsd:string"/> <xsd:element name="validation-class" type="xsd:string"/> <xsd:element name="POJO"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="implementation"/> <xsd:element minOccurs="0" ref="description"/> <xsd:element ref="scope" minOccurs="0"/> <xsd:element minOccurs="0" ref="validator-ref"/> <xsd:element minOccurs="0" ref="methods"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="serialized-param-type" type="xsd:string"/> <xsd:element name="suppress" type="xsd:string"/> <xsd:element name="alias" type="xsd:string"/> <xsd:element name="suppressed-field" type="xsd:string"/> <xsd:element name="suppressed-fields"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="0" ref="suppressed-field" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="serialized-param"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="0" ref="serialized-param-type"/> <xsd:element minOccurs="0" ref="suppress"/> <xsd:element minOccurs="0" ref="alias"/> <xsd:element minOccurs="0" ref="suppressed-fields"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="serialized-params"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="0" ref="serialized-param" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="add-to-session" type="xsd:string"/> <xsd:element name="method"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element minOccurs="0" ref="description"/> <xsd:element minOccurs="0" ref="http-method"/> <xsd:element minOccurs="0" ref="parameters"/> <xsd:element minOccurs="0" ref="add-to-session"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="validators"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="0" ref="validator" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="converters"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="0" ref="converter" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>

ご利用条件 | フィードバック