@Consumes with JSONObject issue

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

@Consumes with JSONObject issue

sudhakar
Hi,

i am trying to post a JSonObject to REST Service,but i am not able to do this.while i am running my client program i  am getting following exception

com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class net.sf.json.JSONObject, and Java type class net.sf.json.JSONObject, and MIME media type, application/json, was not found

here is my restservice :

        import javax.ws.rs.*;
        import javax.ws.rs.ext.Provider;
        import javax.ws.rs.core.MediaType;

        import net.sf.json.JSONObject;

        @Provider
        @Path("/hello")
        public class Hello {

        @POST
        @Consumes("application/json")
        @Produces(MediaType.TEXT_PLAIN)
        public String processMessageObject(JSONObject obj) {
                       
                   return ("Success");
                }
       }

here is my client program :

public class RestJerseyClient {
       
        public static void main(String[] args) {
             JSONObject obj=new JSONObject();
               
                obj.put("ToAddr", "to@tcs");
                obj.put("FromAddr", "from@tcs");
               try{
                       
                        ClientConfig config = new DefaultClientConfig();
                       Client client = Client.create(config);
                        WebResource service = client.resource(getBaseURI());

                    System.out.println("JSON :           "+service.path("rest/hello").type(MediaType.APPLICATION_JSON).post(JSONObject.class,obj));
              }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
               
        }

help me its very urgent
Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

Jairo1
Isn't this a problem of missing
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

?

Jairo
Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

sudhakar
Hi jairo,

even if i add config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
also i am getting same exception.


thanks.

Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

Pengfei Di
In reply to this post by sudhakar
Hi, sudhakar

@Produces(MediaType.TEXT_PLAIN)  =>   @Produces(MediaType.APPLICATION_JSON)


since you specified the type in your client as JSON.

Pengfei




On 04/14/2011 01:55 PM, sudhakar wrote:

> Hi,
>
> i am trying to post a JSonObject to REST Service,but i am not able to do
> this.while i am running my client program i  am getting following exception
>
> com.sun.jersey.api.client.ClientHandlerException: A message body reader for
> Java class net.sf.json.JSONObject, and Java type class
> net.sf.json.JSONObject, and MIME media type, application/json, was not found
>
> here is my restservice :
>
>          import javax.ws.rs.*;
>          import javax.ws.rs.ext.Provider;
>          import javax.ws.rs.core.MediaType;
>
>          import net.sf.json.JSONObject;
>
>          @Provider
>          @Path("/hello")
>          public class Hello {
>
>          @POST
> @Consumes("application/json")
> @Produces(MediaType.TEXT_PLAIN)
> public String processMessageObject(JSONObject obj) {
>
>                     return ("Success");
> }
>         }
>
> here is my client program :
>
> public class RestJerseyClient {
>
> public static void main(String[] args) {
>               JSONObject obj=new JSONObject();
>
> obj.put("ToAddr", "to@tcs");
> obj.put("FromAddr", "from@tcs");
>                 try{
>
> ClientConfig config = new DefaultClientConfig();
>                         Client client = Client.create(config);
> WebResource service = client.resource(getBaseURI());
>
>                      System.out.println("JSON :
> "+service.path("rest/hello").type(MediaType.APPLICATION_JSON).post(JSONObject.class,obj));
>                }
> catch(Exception e)
> {
> e.printStackTrace();
> }
>
> }
>
> help me its very urgent
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Consumes-with-JSONObject-issue-tp6272459p6272459.html
> Sent from the Jersey mailing list archive at Nabble.com.


--
Pengfei Di
Technology

match2blue software development GmbH
Leutragraben 1
07743 Jena

Tel: +49 3641 816 8092
Mobil: +49 1520 166 8691
Fax: +49 3641 573 3479
Email: [hidden email]
Web  : www.match2blue.com
Blog : http://blog.match2blue.com
Registergericht: Amtsgericht Jena
Registernummer: HRB 503726
Geschäftsführerin: Stephanie Renda



Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

Cowtowncoder
In reply to this post by sudhakar
On Thu, Apr 14, 2011 at 4:55 AM, sudhakar <[hidden email]> wrote:
> Hi,
>
> i am trying to post a JSonObject to REST Service,but i am not able to do
> this.while i am running my client program i  am getting following exception
>
> com.sun.jersey.api.client.ClientHandlerException: A message body reader for
> Java class net.sf.json.JSONObject, and Java type class
> net.sf.json.JSONObject, and MIME media type, application/json, was not found

The reason is that neither Jersey nor Jackson directly supports
JSONObject, which is abstraction provided by some other library (json
tools or json util, forget which one).
So if you absolutely have/want to use it, you need to add bindings via
MessageBodyReader/Writer; or declare type as byte[] and handle data
binding yourself.
Latter is quite easy, so I'd probably go with that.

But I suspect you might just want to provide real Java POJOs and not
use library-specific models. There is often no need to use such things
-- that's one of big advantages of JAX-RS over more manual way of
doing things.

-+ Tatu +-
Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

sudhakar
Hi,

thanks for reply,

i am new to RestClint API, can you please help me on how to add bindings via
MessageBodyReader/Writer; or declare type as byte[] and handle data.
simple posting JSONObject to RestService getting delay.

Help me its urgent.


thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

Srinivas Naresh Bhimisetty
Hi Sudhakar,

   please have a look at the entity-provider sample [1] which shows how to implement a MessageBodyReader / MessageBodyWriter.


- Naresh

On Mon, Apr 18, 2011 at 12:04 PM, sudhakar <[hidden email]> wrote:
Hi,

thanks for reply,

i am new to RestClint API, can you please help me on how to add bindings via
MessageBodyReader/Writer; or declare type as byte[] and handle data.
simple posting JSONObject to RestService getting delay.

Help me its urgent.


thanks in advance.

--
View this message in context: http://jersey.576304.n2.nabble.com/Consumes-with-JSONObject-issue-tp6272459p6282540.html
Sent from the Jersey mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

sudhakar
Hi,

even after implementing MessageBodyWriter also i am getting the same like

com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class net.sf.json.JSONObject, and MIME media type, application/json, was not found.

can anyone tell me is there any mistakes in this code.

and My implemented class and Client programs as follows.


@Provider
@Path("/hello")
public class Hello implements MessageBodyWriter<JSONObject>{
       
        MessageBean bean=new MessageBean();
        String res="";
       
        // This method is called if TEXT_PLAIN is request
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String sayPlainTextHello() {
               
                //System.out.println("in TEXT_PLAIN...");
                return "Hello TEXT_PLAIN";
        }

       
        // This method is called if XML is request
        @GET
        @Produces(MediaType.TEXT_XML)
        public String sayXMLHello() {
                return "<?xml version=\"1.0\"?>" + "<hello> Hello TEXT_XML" + "</hello>";
        }

        // This method is called if HTML is request
        @GET
        @Produces(MediaType.TEXT_HTML)
        public String sayHtmlHello() {
                return "<html> " + "<title>" + "Hello TEXT_HTML" + "</title>"
                                + "<body>

" + "Hello Jersey" + "</body>

" + "</html> ";
        }
               
       
        // This method is called if request is POST with JSON
        @POST
        @Consumes("application/json")
        @Produces(MediaType.APPLICATION_JSON)
        //public JSONObject processMessageObject(JSONObject obj) {
        public JSONObject processMessageObject(JSONObject obj) {
                               
                try{
                        //Processing the object
                       
                        //this.bean = (MessageBean) obj;
               
                        //print bean
                // System.out.println("To Address:" + bean.getToAddress());
// System.out.println("From Address:" + bean.getFromAddress());
                       
                        //return ("Success");
                        System.out.println("json content : "+obj.get("ToAddr"));
                        return obj;
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                        //res = res+"To addr : "+bean.getToAddress()+" FromAddr : "+bean.getFromAddress();
                        //return ("Failure");
                        return null;
                }
               
        }

        @Override
        public long getSize(JSONObject arg0, Class<?> arg1, Type arg2,
                        Annotation[] arg3, MediaType arg4) {
                // TODO Auto-generated method stub
                return -1;
        }


        @Override
        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
                        MediaType arg3) {
                // TODO Auto-generated method stub
                //return false;
                return JSONObject.class.isAssignableFrom(arg0);
        }

        @Override
        public void writeTo(JSONObject arg0, Class<?> arg1, Type arg2,
                        Annotation[] arg3, MediaType arg4,
                        MultivaluedMap<String, Object> arg5, OutputStream arg6)
                        throws IOException, WebApplicationException {
                // TODO Auto-generated method stub
               
        }
}


Client Program :

public class RestJerseyClient {
       
        public static void main(String[] args) {
               
                MessageBean msb = new MessageBean();
                msb.setFromAddress("AmericanExpress@email2.americanexpress.com");
                msb.setToAddress("test@customer.com");
                msb.setBccAddress("test");
                msb.setCcAddress("tset");
                msb.setCharSet("test");
                msb.setContentTransferEncoding("ISO-8859-1");
                msb.setContentType(" text/plain");
                msb.setEnvelopeFrom("AmericanExpress@email2.americanexpress.com");
                msb.setFromName("RFC2822");
                msb.setHeaderMap(null);
                msb.setMessageHTML("test");
                msb.setMessageId("CROEGL72377621241774289272");
                msb.setMessageSource("test");
                msb.setMessageText("test");
                msb.setOriginatorName("test");
                msb.setReplyTo("onlinestatements@americanexpress.com ");
                msb.setSubject("Corporate Card Application Approved..Ref#APR01LMTE0001E101");
                msb.setTemplateId("AGNJACRS0028002");
       
               
                try{
                       
                        ClientConfig config = new DefaultClientConfig();
                       
                        config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

                        //config.getClasses().add(JSONObjectProvider.class);
                       
                        //config.getClasses().add(JSONRootElementProvider.class);
                       
                        Client client = Client.create(config);
                        WebResource service = client.resource(getBaseURI());
                       
                                               
                        //JSONConfiguration.natural().build();
                       
                       
                        System.out.println("service : "+service);
               
                        System.out.println("URL : "+ getBaseURI().toString());
                       
                        System.out.println("path : "+service.path("rest"));
                       
                        System.out.println(service.path("rest").path("hello").accept(
                                        MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
                       
                        // Get plain text
                        System.out.println("TEXT_PLAIN : "+service.path("rest").path("hello").accept(
                                        MediaType.TEXT_PLAIN).get(String.class));
                        // Get XML
                        System.out.println("TEXT_XML : "+service.path("rest").path("hello").accept(
                                        MediaType.TEXT_XML).get(String.class));
                        // The HTML
                        System.out.println("TEXT_HTML : "+service.path("rest").path("hello").accept(
                                        MediaType.TEXT_HTML).get(String.class));
                       
                        JSONObject obj=new JSONObject();
                       
                        obj.put("ToAddr", "to@gmail.com");
                        obj.put("FromAddr", "from@gmail.com");
                       
                       
                        System.out.println("POST JSON : "+service.path("rest/hello").type(MediaType.APPLICATION_JSON).post(JSONObject.class,obj));
            //System.out.println("POST JSON : "+service.path("rest/hello").type(MediaType.APPLICATION_JSON).post(MessageBean.class,msb));
               

                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
               
        }

        private static URI getBaseURI() {
                return UriBuilder.fromUri(
                                "http://localhost:9081/de.vogella.jersey.first").build();
        }
       
}

help me...


Reply | Threaded
Open this post in threaded view
|

Re: @Consumes with JSONObject issue

japod
In reply to this post by sudhakar
Hello Sudhakar,

We provide support for org.codehaus.jettison.json.JSONObject
as part of our jersey-json module. I guess it is the "same" class,
only in a different package.

If you can, you may want to switch to using that jettison
based JSONObject. If not, you can get inspired at [1]
on how to write your own Message Body readers/writers.
Look at the com.sun.jersey.json.impl.provider.entity.JSONObjectProvider
class.

HTH,

~Jakub

[1]http://download.java.net/maven/2/com/sun/jersey/jersey-json/1.6/jersey-json-1.6-sources.jar

On 04/14/2011 01:55 PM, sudhakar wrote:

> Hi,
>
> i am trying to post a JSonObject to REST Service,but i am not able to do
> this.while i am running my client program i  am getting following exception
>
> com.sun.jersey.api.client.ClientHandlerException: A message body reader for
> Java class net.sf.json.JSONObject, and Java type class
> net.sf.json.JSONObject, and MIME media type, application/json, was not found
>
> here is my restservice :
>
>          import javax.ws.rs.*;
>          import javax.ws.rs.ext.Provider;
>          import javax.ws.rs.core.MediaType;
>
>          import net.sf.json.JSONObject;
>
>          @Provider
>          @Path("/hello")
>          public class Hello {
>
>          @POST
> @Consumes("application/json")
> @Produces(MediaType.TEXT_PLAIN)
> public String processMessageObject(JSONObject obj) {
>
>                     return ("Success");
> }
>         }
>
> here is my client program :
>
> public class RestJerseyClient {
>
> public static void main(String[] args) {
>               JSONObject obj=new JSONObject();
>
> obj.put("ToAddr", "to@tcs");
> obj.put("FromAddr", "from@tcs");
>                 try{
>
> ClientConfig config = new DefaultClientConfig();
>                         Client client = Client.create(config);
> WebResource service = client.resource(getBaseURI());
>
>                      System.out.println("JSON :
> "+service.path("rest/hello").type(MediaType.APPLICATION_JSON).post(JSONObject.class,obj));
>                }
> catch(Exception e)
> {
> e.printStackTrace();
> }
>
> }
>
> help me its very urgent
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Consumes-with-JSONObject-issue-tp6272459p6272459.html
> Sent from the Jersey mailing list archive at Nabble.com.
>