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();
    }

}
Comments:

Hi Geertjan, can you package this in an EJB module ??

Posted by Luca Masini on November 08, 2012 at 01:27 PM PST #

Can you ?
Is there a full example ?

Regards, f

Posted by guest on June 06, 2013 at 09:22 AM PDT #

Are you ?

Can you read the code above?

Regards, Gj

Posted by Geertjan on June 06, 2013 at 10:37 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
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
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today