Home TOC |
![]() ![]() ![]() |
Types Supported By JAX-RPC
Behind the scenes, the JAX-RPC reference implementation maps types of the Java programming language ("Java types") to XML/WSDL definitions. For example, the reference implementation maps the
java.lang.String
class to thexsd:string
XML data type. Application developers don't need to know the details of these mappings, but they should be aware that not every class in the Java 2 Standard Edition (J2SE) can be used as a method parameter or return type in JAX-RPC.
J2SE SDK Classes
JAX-RPC supports the following J2SE SDK classes:
java.lang.String java.lang.Boolean java.lang.Byte java.lang.Double java.lang.Float java.lang.Integer java.lang.Long java.lang.Short java.lang.String java.math.BigDecimal java.math.BigInteger java.util.Calendar java.util.DateNote that classes in the Java Collections Framework, such as
java.util.ArrayList
, are not supported by JAX-RPC. Unsupported classes can be mapped to XML/WSDL definitions with pluggable serializers and deserializers. However, this mapping technique is for advanced developers and is not covered in this tutorial. For more information on pluggable serializers and deserializers, see the Extensible Type Mapping chapter of the JAX-RPC Specification.Primitives
JAX-RPC supports the following primitive types of the Java programming language:
boolean byte double float int long shortArrays
JAX-RPC also supports arrays with members of supported JAX-RPC types. Examples of supported arrays are
int[]
andString[]
. Multidimensional arrays, such asBigDecimal[][]
, are also supported.For an example of a remote procedure with a
String[]
parameter, take a look at the code sample in thetutorial/examples/jaxrpc/simplebean
directory. In the implementation class namedHelloImpl
, thereverse
method accepts as input aString[]
parameter namedwords
and returns copy of the array in reverse order. The code for thereverse
method follows:public String[] reverse(String[] words) { String[] result = new String[words.length]; int r = 0; for (int w = words.length - 1; w >= 0; w--) { result[r] = words[w]; r++; } return result; }In the following code snippet, the
HelloClient
program invokes thereverse
method and displays the results:private static void demoArray(HelloIF_Stub stub) { try { String[] words = {"it", "was", "a", "dark", "and", "stormy", "night"}; System.out.println("demoArray method:"); for (int j = 0; j < words.length; j++) { System.out.print(words[j] + " "); } System.out.println(); String[] backwards = stub.reverse(words); for (int j = 0; j < backwards.length; j++) { System.out.print(backwards[j] + " "); } System.out.println(); } catch (Exception ex) { ex.printStackTrace(); } }To build, install, and run the example, follow these steps:
- If you haven't already done so, follow the instructions in Setting Up.
- In a terminal window, go to the
docs/tutorial/examples/jaxrpc/simplebean
directory.- Type the following commands:
ant build ant install ant runThe lines displayed by the client should include the following:
demoArray method: it was a dark and stormy night night stormy and dark a was it ...(The other lines displayed are for the JavaBeans component example, which is discussed in a later section.)
Application Classes
JAX-RPC also supports classes that you've written for your applications. In an order processing application, for example, you might provide classes named
Order
,LineItem
, andProduct
. The JAX-RPC Specification refers to such classes as value types, because their values (or states) may be passed between clients and remote services as method parameters or return values.To be supported by JAX-RPC, an application class must conform to the following rules:
- It must have a public default constructor.
- It must not implement (either directly or indirectly) the
java.rmi.Remote
interface.- Its fields must be supported JAX-RPC types.
The class may contain public, private, or protected fields. For its value to be passed (or returned) during a remote call, a field must meet these requirements:
- A public field cannot be final or transient.
- A non-public field must have corresponding getter and setter methods.
JavaBeans Components
JAX-RPC also supports JavaBeans components, which must conform to the same set of rules as application classes. In addition, a JavaBeans component must have a getter and setter method for each bean property. The type of the bean property must be a supported JAX-RPC type.
The code sample in the
tutorial/examples/jaxrpc/simplebean
directory shows how to use a JavaBeans component in a remote call. In this example, the JavaBeans component is calledSimpleAccountBean
:package simplebean; import java.io.Serializable; import java.math.BigDecimal; public class SimpleAccountBean implements Serializable { private BigDecimal balance; private String customerName; public SimpleAccountBean() { BigDecimal balance = new BigDecimal("0.00"); String customerName = null; } public BigDecimal getBalance() { return balance; } public String getCustomerName() { return customerName; } public void setBalance(BigDecimal balance) { System.out.println ("SimpleAccountBean: setting balance to " + balance); this.balance = balance; } public void setCustomerName(String customerName) { System.out.println ("SimpleAccountBean: setting customerName to " + customerName); this.customerName = customerName; } }In the
HelloImpl
class, thecalculateInterest
method has aSimpleAccountBean
parameter:public BigDecimal calculateInterest (SimpleAccountBean simpleAccount) { BigDecimal rate = new BigDecimal("0.05"); BigDecimal factor = rate.add(new BigDecimal("1.00")); BigDecimal newBalance = simpleAccount.getBalance().multiply(factor); return newBalance; }The
HelloClient
program invokes thecalculateInterest
method as follows:private static void demoBean(HelloIF_Stub stub) { try { System.out.println(); System.out.println("demoBean method:"); SimpleAccountBean dukesAccount = new SimpleAccountBean(); dukesAccount.setBalance(new BigDecimal("1200.00")); dukesAccount.setCustomerName("Duke"); BigDecimal newBalance = stub.calculateInterest(dukesAccount); System.out.println("newBalance: " + newBalance); dukesAccount.setBalance(newBalance); } catch (Exception ex) { ex.printStackTrace(); } }To build, install, and run the example, follow these steps:
- If you haven't already done so, follow the instructions in Setting Up.
- In a terminal window, go to the
docs/tutorial/examples/jaxrpc/simplebean
directory.- The code example in the
simplebean
subdirectory demonstrates not only JavaBeans components, but also arrays. If you have already performed the steps in the section Arrays, then you may skip this step. Otherwise, type the following commands:ant build ant install- To run the client, type the following:
ant runThe lines displayed by the client should include the following:
demoBean method: SimpleAccountBean: setting balance to 1200.00 SimpleAccountBean: setting customerName to Duke newBalance: 1260.0000 SimpleAccountBean: setting balance to 1260.0000(The other lines displayed are for the example discussed in the section Arrays.)
Home TOC |
![]() ![]() ![]() |