Message Body Writer

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Message Body Writer

Rimy Sfm
I'm fairly new to restful web services restful and jersey. Within  my web service I have a method that takes 2integers as parameters and I'm getting 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


Can anyone tell me what to do??

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Pavel Bucek-2

Can you share method declaration and optionally client code (or the way
how you are trying to invoke it)? Or whole resource..

BTW, you can check out our samples, we have covered there most of common
usecases, so it is probably you'll find there good starting point for
your application, see [1].

And BTW2, I recommend checking jersey user guide too: [2], especially
part called "Extracting request parameters" [3].

Pavel

[1] http://download.java.net/maven/2/com/sun/jersey/samples/
[2] http://jersey.java.net/nonav/documentation/latest/index.html
[3] http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e246



On 4/13/11 11:03 AM, Rimy Sfm wrote:

> I'm fairly new to restful web services restful and jersey. Within  my web
> service I have a method that takes 2integers as parameters and I'm getting
> 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
>
>
> Can anyone tell me what to do??
>
> Thanks
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Message-Body-Writer-tp6268231p6268231.html
> Sent from the Jersey mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Rimy Sfm
The method declaration:

        @Path("/add")
        @POST
       
        public String add(@FormParam("x")String x,@FormParam("y")String y){
               
                String s = String.valueOf(Integer.parseInt(x)+Integer.parseInt(y));
           return s;}

The client code:


package testClient;


import java.net.URI;


import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.MultivaluedMap;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class test {
        public static void main(String[] args) {
                ClientConfig config = new DefaultClientConfig();
                Client client = Client.create(config);
                WebResource service = client.resource(getBaseURI());
                // Get plain text
                System.out.println(service.path("hello").accept(
                                MediaType.TEXT_PLAIN).get(String.class));
               
               
                // Get XML
                System.out.println(service.path("hello").accept(
                                MediaType.TEXT_XML).get(String.class));
                // The HTML
                System.out.println(service.path("hello").accept(
                                MediaType.TEXT_HTML).get(String.class));
               
       
             
               //Invoking the method returning the sum
               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);
             
        }
       
       
        private static URI getBaseURI() {
                return UriBuilder.fromUri(
                                "http://localhost:8080/exemple.jersey.first/rest/").build();
        }

}
Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Pavel Bucek-2
wrong client code..

try something like:

         ...
         Form form = new Form();
         form.add("a", "2");
         form.add("b", "3");
         ClientResponse responseInt =
service.path("hello/addInt").post(ClientResponse.class, form);
         ...

Pavel

On 4/13/11 12:57 PM, Rimy Sfm wrote:

> The method declaration:
>
> @Path("/add")
> @POST
>
> public String add(@FormParam("x")String x,@FormParam("y")String y){
>
> String s = String.valueOf(Integer.parseInt(x)+Integer.parseInt(y));
>   return s;}
>
> The client code:
>
>
> package testClient;
>
>
> import java.net.URI;
>
> //import javax.ws.rs.core.EntityTag;
> import javax.ws.rs.core.MediaType;
> import javax.ws.rs.core.UriBuilder;
> import javax.ws.rs.core.MultivaluedMap;
>
> import com.sun.jersey.api.client.Client;
> import com.sun.jersey.api.client.ClientResponse;
> import com.sun.jersey.api.client.WebResource;
> import com.sun.jersey.api.client.config.ClientConfig;
> import com.sun.jersey.api.client.config.DefaultClientConfig;
> import com.sun.jersey.core.util.MultivaluedMapImpl;
>
> public class test {
> public static void main(String[] args) {
> ClientConfig config = new DefaultClientConfig();
> Client client = Client.create(config);
> WebResource service = client.resource(getBaseURI());
> // Get plain text
> System.out.println(service.path("hello").accept(
> MediaType.TEXT_PLAIN).get(String.class));
>
>
> // Get XML
> System.out.println(service.path("hello").accept(
> MediaType.TEXT_XML).get(String.class));
> // The HTML
> System.out.println(service.path("hello").accept(
> MediaType.TEXT_HTML).get(String.class));
>
>
> //POST
> MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
>
> formData.add("x", "10");
> formData.add("y", "2");
>
>  ClientResponse response =
> service.path("/hello").path("/add").post(ClientResponse.class,formData);
>
> // EntityTag e = response.getEntityTag();
>  String entity = response.getEntity(String.class);
>  System.out.println (response);
>  System.out.println (entity);
>
>
>
> /*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);*/
>
> }
>
>
> private static URI getBaseURI() {
> return UriBuilder.fromUri(
> "http://localhost:8080/exemple.jersey.first/rest/").build();
> }
>
> }
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Message-Body-Writer-tp6268231p6268493.html
> Sent from the Jersey mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Rimy Sfm
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



Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Suchitha Koneru (sukoneru)
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:[hidden email]]
Sent: Wednesday, April 13, 2011 5:57 AM
To: [hidden email]
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.

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:[hidden email]]
        Sent: Tuesday, March 15, 2011 12:24 AM
        To: [hidden email]
        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:[hidden email]]
        Sent: Montag, 14. März 2011 23:59
        To: [hidden email]
        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
Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Rimy Sfm
Thanks Suchitha,
Do you know if there is a way to implement the mapping of basic data types to a body format ourselves?? how can we implement our own message body reader and message body writer??
Is there any possible solution to this problem??
Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Rimy Sfm
Hey Suchitha,
Can you please send an exemple of message body writer for int.

Thanks in advance.

2011/4/13 Rimy Sfm [via Jersey] <[hidden email]>
Thanks Suchitha,
Do you know if there is a way to implement the mapping of basic data types to a body format ourselves?? how can we implement our own message body reader and message body writer??
Is there any possible olution to this problem?? s


If you reply to this email, your message will be added to the discussion below:
http://jersey.576304.n2.nabble.com/Message-Body-Writer-tp6268231p6269902.html
To unsubscribe from Message Body Writer, click here.

Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

satishrana01@live.com
In reply to this post by Suchitha Koneru (sukoneru)
use following MediaType for java

ClientResponse response = service.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);
Reply | Threaded
Open this post in threaded view
|

Re: Message Body Writer

Rimy Sfm
Thanks satishrana, do I use this for any type java?

2011/6/1 [hidden email] [via Jersey] <[hidden email]>
use following MediaType for java

ClientResponse response = service.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);



If you reply to this email, your message will be added to the discussion below:
http://jersey.576304.n2.nabble.com/Message-Body-Writer-tp6268231p6425915.html
To unsubscribe from Message Body Writer, click here.

Reply | Threaded
Open this post in threaded view
|

(no subject)

Rimy Sfm
This post has NOT been accepted by the mailing list yet.
In reply to this post by Rimy Sfm

Reply | Threaded
Open this post in threaded view
|

(no subject)

Rimy Sfm
This post has NOT been accepted by the mailing list yet.
In reply to this post by Rimy Sfm