Tuesday Nov 06, 2012

EJB Exposed As RESTful Web Service

Here's the kind of URL I want to support:

http://localhost:12663/myapp/webresources/customers/Miami 

And here's the EJB that exposes a method as a RESTful web service:

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/customers")
@Stateless
public class CustomerSessionBean {

    @PersistenceContext
    EntityManager em;
    
    @GET
    @Path("{city}")
    @Produces("application/xml")
    public List<Customer> getCustomersByCity(@PathParam("city") String city) {
        Query cityQuery = em.createNamedQuery("Customer.findByCity");
        cityQuery.setParameter("city",city);
        return cityQuery.getResultList();
    }
    
    public List<Customer> getCustomers() {
        return em.createNamedQuery("Customer.findAll").getResultList();
    }

}
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« November 2012 »
SunMonTueWedThuFriSat
    
11
13
14
15
24
25
 
       
Today