WebLogic Web サービス プログラマーズ ガイド
![]() |
![]() |
![]() |
![]() |
この章では、WebLogic Web サービス API の使い方に関する情報を提供します。
WebLogic Web サービスには、JAX-RPC API と SAAJ API のほかにも、さまざまなタスクに使用できる API が用意されています。通常、これらの API はクライアント アプリケーションで使用しますが、EJB などのサーバサイド コンポーネントで使用できる場合もあります。
各節では、WebLogic Web サービス API の主な使い方について説明します。API の詳細なリファレンス情報については、Javadoc を参照してください。
次の表に、WebLogic Web サービス API と、その主な使い方を示します。
|
|
|
|
|
|
|
|
clientgen
Ant タスクで生成した JAR ファイルのスタブには、Web サービス オペレーションのパラメータまたは戻り値として使用される非組み込みデータ型のマッピングと登録をクライアント アプリケーションが処理するために必要な Java コードが含まれています。しかし、Web サービスを呼び出す動的クライアントを記述する場合、動的クライアントでは clientgen
によって生成されたスタブが使用されないため、開発者自身がデータ型のマッピングと登録を行う必要があります。
データ型のマッピングを登録する標準的な方法は、JAX-RPC TypeMappingRegistry
クラスを使用することです。シリアライゼーション クラスは常に手動で登録する必要がありますが、シリアライゼーション クラスの一部は WebLogic の内部クラスであるため、このクラスは使いづらいという難点があります。次の例では、TypeMappingRegistry
クラスの使い方を示します。該当するコード箇所は太字になっています。
package examples.jaxrpc.call3;
import javax.xml.soap.SOAPConstants;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Service;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import org.soapinterop.xsd.SOAPStructCodec;
import org.soapinterop.xsd.SOAPStruct;
public class MSInterop{
public static void main( String[] args ) throws Exception{
// weblogic ServiceFactory を設定
System.setProperty( "javax.xml.rpc.ServiceFactory",
"weblogic.webservice.core.rpc.ServiceFactoryImpl" );
// サービス ファクトリを作成
ServiceFactory factory = ServiceFactory.newInstance();
// QName を定義
String targetNamespace = "http://soapinterop.org/";
QName serviceName = new QName( targetNamespace, "SimpleTest" );
QName portName = new QName( targetNamespace, "SimpleTestSoap" );
QName operationName = new QName( "http://soapinterop.org/",
"echoStruct" );
// サービスを作成
Service service = factory.createService( serviceName );
TypeMappingRegistry registry = service.getTypeMappingRegistry();
TypeMapping mapping = registry.getTypeMapping(
SOAPConstants.URI_NS_SOAP_ENCODING );
mapping.register( SOAPStruct.class,
new QName( "http://soapinterop.org/xsd", "SOAPStruct" ),
new SOAPStructCodec(),
new SOAPStructCodec() );
// 呼び出しを作成
Call call = service.createCall();
// ポートとオペレーション名を設定
call.setPortTypeName( portName );
call.setOperationName( operationName );
call.addParameter( "inputStruct",
new QName( "http://soapinterop.org/xsd", "SOAPStruct" ),
ParameterMode.IN);
call.setReturnType(
new QName( "http://soapinterop.org/xsd", "SOAPStruct" ) );
// エンド ポイント アドレスを設定
call.setTargetEndpointAddress(
"http://www.mssoapinterop.org/asmx/simple.asmx" );
SOAPStruct s = new SOAPStruct();
s.setVarInt(2);
s.setVarString("foo");
s.setVarFloat(123123);
System.out.println(s.toString());
SOAPStruct res = (SOAPStruct) call.invoke(new Object[]{s} );
System.out.println( res );
}
}
WebLogic Web サービスには、動的クライアントで非組み込みデータ型のマッピング登録を簡単に実行できる「weblogic.webservice.encoding」パッケージが用意されています。この API には、以下の 2 つのメイン クラスが定義されています。
DefaultTypeMapping
: XML データ型の登録、および対応する Java データ型との関連付けに使用します。GenericTypeMapping
: すべての非組み込み XML データ型と汎用の Java データ型 SOAPElement
との関連付けに使用します。次の手順では、動的クライアント アプリケーションでの DefaultTypeMapping
クラスの使い方について説明します。
autotype
Ant タスクを実行して、シリアライゼーション クラス、Java クラス、XML スキーマ、および Web サービスで使用する非組み込みデータ型用の types.xml
ファイルを作成します。この例では、types.xml
ファイルを mydir
ディレクトリに作成します。詳細については、「autotype Ant タスクを実行する」を参照してください。
import weblogic.webservice.encoding.GenericTypeMapping;
import weblogic.webservice.encoding.DefaultTypeMapping;
TypeMappingRegistry registry = service.getTypeMappingRegistry();
registry.registerDefault(
new DefaultTypeMapping( "mydir/types.xml" ) );
「weblogic.webservice.context.WebServiceSession」クラスを使用して、HTTP セッションを管理する WebLogic Server 内部のインフラストラクチャにアクセスします。WebLogic Web サービスはサーブレットではなく Java クラスおよび EJB により実装されるため、その HTTP セッション情報を直接的に利用することはできません。そのため、weblogic.webservice.context
パッケージを使用してアクセスする必要があります。WebServiceSession
の使い方については、「javax.servlet.http.HttpSession」の Javadoc を参照してください。
Web サービスの実装で、WebServiceContext
オブジェクトから WebServiceSession
オブジェクトを取得し、次に、標準の getAttribute()
メソッドおよび setAttribute()
メソッドを使用して、セッションの属性を取得および設定します。
注意 :このセッションが正しく動作するには、クライアント アプリケーション側で HTTP クッキーがサポートされている必要があります。
次の例では、weblogic.webservice.context
API を使用している Web サービスを実装する Java クラスのメソッドを示します。このメソッドは、Web サービス オペレーションの複数の呼び出し間でセッション ステートを維持します。
import weblogic.webservice.context.WebServiceContext;
import weblogic.webservice.context.ContextNotFoundException;
import weblogic.webservice.context.WebServiceSession;
....
/*
* 複数の呼び出し間でセッション ステートを維持する
* HTTP セッションの使い方を示す
*/
public int maintainSessionState(){
try{
WebServiceContext wsContext = WebServiceContext.currentContext();
WebServiceSession session = (WebServiceSession)wsContext.getSession();
Integer count = (Integer)session.getAttribute( "count" );
count = (count==null) ?
new Integer( 0 ) : new Integer( count.intValue() + 1 );
session.setAttribute( "count", count );
return count.intValue();
}catch( ContextNotFoundException e ){
e.printStackTrace();
return -1;
}
}
呼び出し対象となる Web サービスの WSDL を参照するには、動的クライアント アプリケーションで「weblogic.webservice.extensions」パッケージを使用します。特に、WLCall
インタフェースは javax.xml.rpc.Call
インタフェースを拡張することで、以下の機能が追加されています。
次の例では、weblogic.webservice.extensions.WLCall
インタフェースを使用して、呼び出し対象であるオペレーションのパラメータの詳細を、クライアント アプリケーションで取得する方法を示します。
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.Call;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import weblogic.webservice.encoding.GenericTypeMapping;
import weblogic.webservice.extensions.WLCall;
public class BrowserClient{
public void invoke() throws RemoteException, InvokeFailedException,
ServiceException, IOException{
String url = "http://localhost:7001/mega/TemperatureService?WSDL";
System.setProperty( "javax.xml.rpc.ServiceFactory",
"weblogic.webservice.core.rpc.ServiceFactoryImpl" );
System.setProperty( "weblogic.webservice.servicenamechecking",
"false" );
ServiceFactory factory = ServiceFactory.newInstance();
QName serviceName = new QName( "http://www.bea.com/mega-service/",
"MegaWebService" );
Service service = factory.createService( new URL( url ), serviceName );
TypeMappingRegistry registry = service.getTypeMappingRegistry();
registry.registerDefault( new GenericTypeMapping() );
System.out.println( "+ Service: " + service.getServiceName() );
for( Iterator it = service.getPorts(); it.hasNext(); ){
QName portName = (QName)it.next();
System.out.println( " + Port: " + portName );
Call[] calls = service.getCalls( portName );
printCalls( calls );
}
}
private void printCalls( Call[] calls ){
for( int i=0; i<calls.length; i++ ){
Call call = calls[i];
System.out.println( " + Operation :" + call.getOperationName() );
printParameters( (WLCall)call );
if( call.getReturnType() != null ){
System.out.println( " + Return Type:" + call.getReturnType() );
}
System.out.println( "" );
}
}
private void printParameters( WLCall call ){
for( Iterator it = call.getParameterNames(); it.hasNext(); ){
String name = (String)it.next();
System.out.println( " + Part :" + name );
System.out.println( " - Java Type :" +
call.getParameterJavaType( name ) );
System.out.println( " - Mode :" +
call.getParameterMode( name ) );
System.out.println( " - XML Type :" +
call.getParameterTypeByName( name ) );
}
}
}
![]() ![]() |
![]() |
![]() |