wiki:ExampleUsingOwnConceteTypes

Version 1 (modified by lauer, 17 years ago) (diff)

--

Using own concete types

Server side

public interface Api
{
    Param returnParam();
    
    void passAsParameter( Param p );
}

public class Impl implements Api
{
    public Param returnParam() { return new ParamImpl( ... ); }
    
    void passAsParameter( Param p ) {}
}

To use type Param, we have to make it XML-RPC compliant. We use java annotations and user-defined conversion operations to do this.

  • the @XmlRpc annotation declares that type Param uses XML-RPC type ARRAY as transport representation
  • The interface {{{Convertable}} declares what specific java type is used for transfer via XML-RPC
  • toXmlRpc converts an instance of type Param into its XML-RPC representation
  • the constructor creates an instance of type Param back from it's XML-RPC representation
@XmlRpc( type=Type.ARRAY )      
public class Param 
               implements Convertable<Collection<String>>
{
    public Param( Collection<String> xmlRpcRepresentation ) { ... }

    public Collection<String> toXmlRpc() { return ... }
}

Now our type ist XML-RPC compliant!

Client Side

Our client can be used without any extra statements:

Api remote_api = XmlRpc.createClient( Api.class, "handlerId", host, port );

Param p = remote_api.returnParam();

Param asParam = ...;
remote_api.passAsParameter( asParam );

...