A @WebService as a @Stateless Session EJB

Why should SOAP-based web services be deployed as EJBs instead of as POJOs? One answer is quite practical. If an organization already has stateless Session EJBs in place, these can become SOAP-based services as well by adding the @WebService annotation to the EJB code; no other change to the EJB code is needed. At a more technical but still practical level, the EJB container is programmer-friendly in handling issues such as thread safety. A web container such as Tomcat’s Catalina or Jetty does not provide thread safety for web sites and web services, but an EJB container does provide such safety. Concurrency issues are formidable challenges in Java, and it is appealing to offload such challenges from the programmer to the container. GlassFish and other JASes also offer first-rate development support for web services deployed as @Stateless Session EJBs. Upon successful deployment of the service, for example, GlassFish generates a web page that can be used to test all of the service operations; another page to inspect the WSDL; another page to inspect performance; and so on. In any event, the good news is that Java programmers have options about how to implement SOAP-based as well as REST-style web services. This section explores one such option for SOAP-based services: the @Stateless Session EJB. The EJB example also uses a database for persistence, which is an opportunity to illustrate how the @Entity annotation works. For reference, this ...

Get Java Web Services: Up and Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.