# Web Services support

EJBs provide seamless support for exposing business functionality as web services - REST and SOAP being two variants. The annotations responsible to this magic are

* `@Path`
* `@WebService`

> `@Path` and `@WebService` are not a part of the EJB specification. They belong to JSR 311 (JAX-RS) and JSR 181 (JAX-WS) specifications respectively

## `@WebService`

The real beauty lies in the fact that session beans can be decorated with this annotation and the EJB container **automagically** exposes their functionality as SOAP based web services.

```
package ejbap.ws;

import java.util.Date;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;


@Stateless
@WebService
public class EJBasSOAPWebService {

    @WebMethod
    public String getDate(){
        return new Date().toString();
    }

    @WebMethod
    @WebResult(name = "response")
    public String greet(@WebParam String greeting){
        return greeting + " World !";
    }
}
```

As you might have already noticed in the above example, JSR 181 exposes other annotations like `@WebMethod`, `@WebResult`, `@WebParam` etc. to help further refine your web service methods.

> In order to gain more flexibility and ease of extension, a better approach would be to provide a layer of indirection with the help of an additional session bean
>
> * Delegate actual service calls to an injected EJB instance
> * Avoid tight coupling between your Web Service layer and EJB business tier logic

## `@Path`

REST web services cannot lag behind SOAP. Can they ! Java EE provides you the ability to use the `@Path` annotation to expose session beans over a RESTful interface. I guess you have already realized that the idea is not very different compared to the SOAP based web service interface approach.

```
package ejbap.ws;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;


@Stateless
@Path("test")
public class EJBonREST {

    @GET
    public String get(@QueryParam("name") String propName){
        return System.getProperty(propName);
    }

    @POST
    public String greet(final String greeting){
        return greeting + " World !";
    }

}
```

> Similar to SOAP based approach
>
> * Avoid coupling b/w your EJB and RESTful interface by introducing  another layer of abstraction/facade
> * Continue to enrich your EJB methods with additional annotations from the JAX-RS API e.g. `@PUT`, `@DELETE`, `@PathParam` etc.

## Up ahead...

The next chapter covers exception handling and its one and only annotation
