users@jersey.java.net

[Jersey] Re: com.sun.jersey.api.MessageException ?

From: Martin Matula <martin.matula_at_oracle.com>
Date: Thu, 23 Jun 2011 13:49:26 +0200

This can't work - you would either have to use multipart media type to
send several images, or come up with some custom format or send the
pictures one by one - Jersey has no idea how to serialize ArrayList of
binary data and even if it tried guessing what you mean, the result
would definitely not conform to "image/jpeg" type.
So, you can do one of the following:
- return a list of URI's of the images, then do separate requests to
retrieve each image, or,
- use multipart to fit several images into one message
- come up with your own serialization of the list of images, write your
own message body reader/writer for that. No other client would
understand that.
Martin

On 23.6.2011 9:02, susmitha wrote:
> Hello , i want to retrieve pictures from database and display list of images
> using a web service. Logic is fine n i am able to retrieve pics from
> database which are stored as Blob type. Everything is working fine but web
> service giving "com.sun.jersey.api.MessageException". Return type is
> ArrayList<Inputstream>.
>
> My java code is:
>
> package hai;
>
> import java.io.InputStream;
> import java.sql.Blob;
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.sql.Statement;
> import java.util.ArrayList;
> import javax.xml.bind.annotation.XmlRootElement;
> @XmlRootElement
>
>
> public class TestBlob
> {
>
> Connection con;
> String url = "jdbc:mysql://localhost:3306/divinator";
> String Sqlusername ="root";
> String Sqlpassword ="root";
> Blob image=null;
>
>
>
>
> public ArrayList<InputStream> testImage() throws SQLException,
> ClassNotFoundException
>
> {
> ArrayList<Blob> imgData=new ArrayList<Blob> ();
> java.sql.Blob b=null;
> ArrayList<InputStream> is= new ArrayList<InputStream>();
>
>
> try
> {
> Class.forName("com.mysql.jdbc.Driver");
> con = DriverManager.getConnection(url, Sqlusername, Sqlpassword);
> Statement stmt = con.createStatement();
> String query = "select * from pictures where id=1";
> ResultSet rs = stmt.executeQuery(query);
>
> while (rs.next ())
> {
>
> imgData .add(rs.getBlob("pic1"));
> imgData .add(rs.getBlob("pic2"));
> imgData .add(rs.getBlob("pic3"));
> imgData .add(rs.getBlob("pic4"));
> imgData .add(rs.getBlob("pic5"));
>
>
> System.out.println("imgData :"+imgData);
>
>
> }
> int i=imgData.size();
> System.out.println("size:"+i);
>
> for(int j=0; j&lt;i;j++)
> {
> b=imgData.get(j);
> InputStream str=null;
> str=b.getBinaryStream();
> System.out.println(&quot;blob is:&quot;+b);
> System.out.println(&quot;inputstream :&quot;+str);
> is.add(str);
> System.out.println(&quot;is:&quot;+is);
> }
>
> }
>
>
> catch (Exception e)
>
> {
> e.printStackTrace();
>
> return null;
> }
>
>
> return is;
> }
>
>
> public static void main(String args[]) throws SQLException,
> ClassNotFoundException
> {
>
> System.out.println(&quot;from main :&quot; );
>
> TestBlob b=new TestBlob();
> b.testImage();
>
> }
>
> }
>
>
> *****************************************
>
>
> And My web service code is:
>
>
> import hai.GetBlob;
> import hai.TestBlob;
>
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.io.InputStream;
> import java.sql.SQLException;
> import java.util.ArrayList;
>
> import javax.ws.rs.GET;
> import javax.ws.rs.Path;
> import javax.ws.rs.Produces;
> import javax.ws.rs.QueryParam;
> import javax.xml.bind.annotation.XmlRootElement;
>
> @XmlRootElement
>
>
> @Path(&quot;service&quot;)
> public class webservice
>
> {
>
> @GET
> @Path(&quot;testimage&quot;)
> @Produces(&quot;image/jpeg&quot;)
>
> public static ArrayList&lt;InputStream&gt; getTestImage() throws
> FileNotFoundException
>
> {
> ArrayList<InputStream> is=null;
>
>
>
>
> try
>
> {
>
>
> TestBlob iw=new TestBlob();
> is = iw.testImage();
> System.out.println("from web service:"+is);
>
> }
>
> catch (SQLException e)
>
> {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> catch (ClassNotFoundException e)
>
> {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> return is;
> }
>
>
> }
>
>
>
> **************************************
>
>
> Suggest me plz.
>
>
>
>
>
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/com-sun-jersey-api-MessageException-tp6507380p6507380.html
> Sent from the Jersey mailing list archive at Nabble.com.