users@jersey.java.net

[Jersey] Re: Message Body Writer

From: Suchitha Koneru (sukoneru) <"Suchitha>
Date: Wed, 13 Apr 2011 10:02:14 -0700

Hello Rimy,

We cannot return any of the basic data types (Integer, Short, Float
etc..) because there is no default mapping of basic data types to any
particular body format. Please see the attached mail

Thanks,
Suchitha



-----Original Message-----
From: Rimy Sfm [mailto:atitallah.rim_at_gmail.com]
Sent: Wednesday, April 13, 2011 5:57 AM
To: users_at_jersey.java.net
Subject: [Jersey] Re: Message Body Writer

I'm sorry I posted a wrong method declaration. Actually the method
causin me
the problem takes 2 integer as paramerter:


        @Path("/addInt")
        @POST
        public Integer add(@FormParam("a") Integer a,
@FormParam("b")Integer b)
        {
                return a+b;
        }


and this is the part of my client code whre I invoke it:


                                MultivaluedMapImpl formDataInt = new
MultivaluedMapImpl();
                                  
                                        formDataInt.add("a", 2);
                                        formDataInt.add("b", 3);
                                        ClientResponse responseInt =
service.path("hello/addInt").post(ClientResponse.class,formDataInt);
                                        String entityInt =
responseInt.getEntity(String.class);
                                        System.out.println
(responseInt);
                                        System.out.println (entityInt);
              
        }

And when I run my server I get this error:

GRAVE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException:
com.sun.jersey.api.MessageException: A
message body writer for Java class java.lang.Integer, and Java type
class
java.lang.Integer, and MIME media type application/octet-stream was not
found




--
View this message in context:
http://jersey.576304.n2.nabble.com/Message-Body-Writer-tp6268231p6268855
.html
Sent from the Jersey mailing list archive at Nabble.com.

attached mail follows:



Hi Suchitha,

yes, you will need a separate writer for both single element and list.
To be able to consume such data, you will also need to implement
appropriate readers (MessageBodyReader).

~Jakub

On 03/16/2011 05:36 PM, Suchitha Koneru (sukoneru) wrote:

        Thanks Markus for the response. If I were to develop message body writers for basic data types (int, long ) etc should I develop two for each data type

        (one for basic data type and one for collection) , i.e.

         

        public class LongMesgWriter implements MessageBodyWriter<Long>

         

                        and

         

        public class LongCollMesgWriter implements MessageBodyWriter<Collection<Long>>

         

         

        could you please let me know

         

        thanks,

        Suchitha

         

        From: Markus Karg [mailto:karg_at_quipsy.de]
        Sent: Tuesday, March 15, 2011 12:24 AM
        To: users_at_jersey.java.net
        Subject: [Jersey] Re: returning basic data types

         

        There is no default mapping of basic data types to any particular body format, so you'll either can return a String instead, or you can provide a custom MessageBodyWriter which tells JAX-RS how the body format shall look like.

         

        From: Suchitha Koneru (sukoneru) [mailto:sukoneru_at_cisco.com]
        Sent: Montag, 14. März 2011 23:59
        To: users_at_jersey.java.net
        Subject: [Jersey] returning basic data types

         

        Hello Jersey Users ,

                The following apis do not work, we are using Jersey 1.5 on Jboss with JDK 1.6; Is there any other way of returning basic data types (int, byte , short etc) ? I also tried changing the mime type to application/xml which does not help.

         

         

              @GET

              @Path("testLongWrapper")

              @Produces("application/text")

              

          public Long getLongBasic(){

                return new Long(1234);

          }

         

                

         

              @GET

              @Path("testLongBasic")

              @Produces("application/text")

              

          public long getLong(){

                return new Long(1234);

          }

         

        Thanks,

        Suchitha



-- 
Jakub Podlesák
CZJUG co-lead,
Web Services Research And Development
Oracle, Czech s r.o.
Praha 4, V Parku 8