users@jersey.java.net

[Jersey] Re: _at_Consumes with JSONObject issue

From: sudhakar <sudhakar.dunaka_at_tcs.com>
Date: Tue, 19 Apr 2011 00:19:31 -0700 (PDT)

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&lt;String, Object&gt; 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_at_email2.americanexpress.com");
                msb.setToAddress("test_at_customer.com");
                msb.setBccAddress("test");
                msb.setCcAddress("tset");
                msb.setCharSet("test");
                msb.setContentTransferEncoding("ISO-8859-1");
                msb.setContentType(" text/plain");
                msb.setEnvelopeFrom("AmericanExpress_at_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_at_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_at_gmail.com");
                        obj.put("FromAddr", "from_at_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...




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