Tuesday, September 2, 2008

rules for writing a web service endpoint interface and use of the @WebService and @WebMethod annotations.

Identify correct and incorrect statements or examples about implementing a session bean as a web service endpoint, including rules for writing a web service endpoint interface and use of the @WebService and @WebMethod annotations.

"To support web service interoperability, the EJB specification requires compliant implementations tosupport XML-based web service invocations using WSDL and SOAP or plain XML over HTTP in conformance with the requirements of the JAX-WS [32], JAX-RPC [25], Web Services for Java EE [31], and Web Services Metadata for the Java Platform [30] specifications."


You know, it's amazing, but turning Stateless Session EJBs into Web Services is just as simple as adding an annotation after the @Stateless decoration. Here's the little EJB I created to test the @WebService annotation. All I had to do was add the annotation, deploy, and I was able to test it with WebSphere 7's Web Services explorer.

package com.mcnz.ejb;

import javax.ejb.Stateless;
import javax.jws.WebService;


@Stateless
@WebService
public class StatelessTimerBean implements StatelessTimerLocal {


public StatelessTimerBean() {
}

public String helloWorld() {
return "Hello World";
}
}


One thing I didn't really understand was how the EJB container knew how to deploy my EJB as a web service. I mean, I guess it just deployed the EJB, but how did all the Web Service razzle dazzle happen? I mean, that has to go through an HTTP port, right? So does that mean an EJB3 SLSB with a @WebService decoration uses a web project or web container? I'm perplexed right now, but I'll dig into the WebSphere Application Server and see exactly what is going on behind the scenes. Here's what the spec says:

"Session Bean’s Web Service Endpoint Interface

EJB 3.0 does not require the definition of a web service endpoint interface for session beans that implement a web service endpoint.


The following are requirements for session beans with JAX-RPC web service endpoint interfaces. The JAX-WS and Web Services for Java EE specifications do not require that a separate interface be defined for a web service endpoint.

The following are the requirements for a stateless session bean’s web service endpoint interface. The web service endpoint interface must follow the rules for JAX-RPC service endpoint interfaces.

• The web service endpoint interface must extend the java.rmi.Remote interface.
• The methods defined in the interface must follow the rules for JAX-RPC service endpoint interfaces. This means that their argument and return values must be of valid types for JAX-RPC, and their throws clauses must include the java.rmi.RemoteException.

The throws clause may additionally include application exceptions.

Note that JAX-RPC Holder classes may be used as method parameters. The JAX-RPC specification requires support for Holder classes as part of the standard Java mapping of WSDL operations in order to handle out and inout parameters. Holder classes implement the javax.xml.rpc.holders.Holder interface

• For each method defined in the web service endpoint interface, there must be a matching method in the session bean’s class. The matching method must have:

• The same name.
• The same number and types of arguments, and the same return type.
• All the exceptions defined in the throws clause of the matching method of the session bean class must be defined in the throws clause of the method of the web ser vice endpoint interface.
• The web service endpoint interface must not include an EJBObject or EJBLocalObject as either a parameter or return type. An array or JAX-RPC value type must not include an EJBObject or EJBLocalObject as a contained element. The web service endpoint interface methods must not expose business interface types, local or remote interface types, local or remote home interface types, timers or timer handles, or the managed collection classes that are used for entity beans with container-managed persistence as arguments or results or as fields of value types.
• JAX-RPC serialization rules apply for any value types that are used by the web service endpoint interface. If it is important that Java serialization semantics apply, the Bean Provider should use the restricted set of JAX-RPC value types for which the semantics of Java serialization apply under JAX-RPC serialization. See the JAX-RPC specification [25] for details.
• The web service endpoint interface must not include constant (as public final static) declarations.
• The Bean Provider must designate the web service endpoint interface in the deployment descriptor by means of the service-endpoint element. The service endpoint itself is only exposed within a web service if it is referenced by a web service deployment descriptor.


The implementation class for a stateless session bean’s web service endpoint is generated by the container’s deployment tools. This class must handle requests to the web service endpoint, unmarshall the SOAP request, invoke any business method interceptor methods, and invoke the stateless session bean method that matches the web service endpoint method that corresponds to the request."

No comments: