pvillega’s posterous

pvillega’s posterous

Pere Villega  //  Born in Barcelona, living in Dublin, and tagged as geek since youth. Developer in the path to becoming a software architect. I swear this is not a proper blog :)

Feb 16 / 7:28am

Web services (JAX-WS)

This page contains a brief summary of Web services. It only gives basic information, for more details about life cycle and other functionalities check the EJB 3.0 reference or some book like EJB3 in Action.

Java gives support to several implementations of web services, as REST, XML-RPC or SOAP. Sadly JEE 5 doesn't require support for REST so it's covered by proprietary extensions, but it covers pretty well XML-RPC and specially SOAP using JAX-WS. In this section I won't discuss the particulars of web services like UDDI or WSDL. Suffice to say JAX-WS 2.0 is the API to use for managing web services and it provides compatibility with JEE 1.4 XML-RPC calls although it favours SOAP web services. This library allows you to use both POJOs or EJB as web services. Although each one has its advantages (like POJOs being able to run in tomcat while EJB aren't) the usage of EJB as web services is recommended as you obtain extra container functionalities like timers, transactions, security and more that plain POJOs lack.

An example of stateless Session bean being exposed as web service:

//We use annotations to generate our web service interface over a stateless bean
//WebService is the only required annotation and defines de urn
//SOAPBinding can be document (SOAP) or RPC (XML-RPC)

@WebService(targetNamespace ="urn:DemoWebService")
@SOAPBinding( style = SOAPBinding.Style.DOCUMENT)
@Stateless
public class WebServiceBean implements WebServiceLocal {

//these annotations allows us to control the names of the params and return value
//of the exposed method
@WebMethod
@WebResult (name = "webNumber")
public Long webMethod(@WebParam(name = "user") String user) {
return Long.MAX_VALUE;
}
}

The only required annotation is @WebService. Other annotation allows us to configure details like the binding (SOAP or RPC), which methods are exposed and the names of the parameters in the WSDL.

@WebService can be added to the bean or to one interface. If added to the interface all its methods will be exposed, while if we use it on the bean it will expose all the bean methods, unless we mark them explicitly with @WebMethod to identify the ones which should be exposed. @WebMethod can also be used to mark methods that must be hidden user the parameter "exclude" in the annotation.

@WebParam can be used to customize parameters (and @WebResult for the return value) of a method. It allows to change the exposed name and the mode (IN, OUT, INOUT) of each parameter along with other properties. There are some other annotations in the specification, like @OneWay, used to mark method with no output so they are optimized, or @HandlerChain to set up web service specific interceptors.

To consume those web services, you can use the @WebServiceRef annotation both in Java clients or EJB. In the latter case the annotation can be used to perform dependency injection.

Loading mentions Retweet

Filed under // ejb java jax-ws web service

Comments (0)