dev@jersey.java.net

Re: Reposting to jersey alias- REST Resources talking to Session EJB

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 15 Feb 2008 11:09:06 +0100

Hi Ayub,

The example does not work because the Jersey runtime currently does not
support the injection of references to EJBs, hence the
FlowerService2Resource.ejbRef field is null.

I don't want to implement all the EE compliant support in Jersey and
instead want to defer to an IoC framework like Spring, Guice or WebBeans
for such support where such support can be plugged in and Jersey does
not bind to a particular IoC framework.

BTW if you want to improve the example for returning an image you could
write your own message body writer that supports writing of the Image class.

    @GET
    @ProduceMime("image/png")
    public String getImage(String name) throws IOException {
      byte[] bytes = ejbRef.getFlower(name);
      return getImage(bytes, false);
    }

The Mandel example in the Jersey distribution has a message body writer
that does just that.

In fact i would write the RESTful service differently to that of the
FlowerService Web service, and would return a list of links for the
request of the list of the thumbnail images maybe as HTML and XML, or if
you want to embed the list of images i would use multipart mime, or you
can do all three :-)

Paul.


Ayub Khan wrote:
> Hi Paul,
>
> Using NB6.1 m1 (jersey 0.5), I created a JAX-WS service that talks to a
> Session EJB, following the tutorial (steps 1-3, ie till testing the app)
>
> http://www.netbeans.org/kb/60/websvc/ejb.html#Exercise_3.
>
> Found JAX-WS to EJB working fine.
>
> So now I wanted to test the scenario with a J2EE app 'FlowerApplication'
> containing EJB module 'FlowerAlbum' and REST web project 'FlowerService2'.
>
> Steps
> 1. I created a web app 'FlowerService2' with a single Resource as follows.
> 2. Also added library reference to the EJB module FlowerAlbum.
> 3. Created a new J2EE app 'FlowerApplication2' , then wrapped EJB module
> 'FlowerAlbum' with 'FlowerService2' REST web project.
> 4. Deployed J2EE app.
> 5. Test the url
> 'http://localhost:8080/FlowerService2/resources/flowerService2/
>
> I got a NPE at line 'byte[] bytes = ejbRef.getFlower(name);', in the
> getImage REST method, what
> could be the problem.
>
> flower.album.FlowerService2Resource.java
> =====================================
>
> @Path("flowerService2")
> public class FlowerService2Resource {
> @HttpContext
> private UriInfo context;
> @EJB
> private FlowerRemote ejbRef;
>
> /** Creates a new instance of FlowerService2Resource */
> public FlowerService2Resource() {
> }
>
> /**
> * Retrieves representation of an instance of
> flower.album.FlowerService2Resource
> * @return an instance of java.lang.String
> */
> @GET
> @ProduceMime("text/html")
> public String getImage(String name) {
> try {
> byte[] bytes = ejbRef.getFlower(name);
> Image img = getImage(bytes, false);
> return img.getSource().toString();
> } catch (IOException ex) {
>
> Logger.getLogger(FlowerService2Resource.class.getName()).log(Level.SEVERE,
> null, ex);
> }
> return null;
> }
> private Image getImage(byte[] bytes, boolean isThumbnail) throws
> IOException {
> ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
> Iterator readers = ImageIO.getImageReadersByFormatName("png");
> ImageReader reader = (ImageReader) readers.next();
> Object source = bis; // File or InputStream
> ImageInputStream iis = ImageIO.createImageInputStream(source);
> reader.setInput(iis, true);
> ImageReadParam param = reader.getDefaultReadParam();
> if (isThumbnail) {
> param.setSourceSubsampling(4, 4, 0, 0);
> }
> return reader.read(0, param);
> }
> }
> =================================
>
> Thanks
> Ayub
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: dev-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109