[Jersey] Re: _at_Consumes with JSONObject issue

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


even after implementing MessageBodyWriter also i am getting the same like

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.

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

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

        // This method is called if HTML is request
        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
        //public JSONObject processMessageObject(JSONObject obj) {
        public JSONObject processMessageObject(JSONObject obj) {
                        //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)
                        //res = res+"To addr : "+bean.getToAddress()+" FromAddr :
                        //return ("Failure");
                        return null;

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

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

        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.setContentType(" text/plain");
                msb.setReplyTo(" ");
                msb.setSubject("Corporate Card Application
                        ClientConfig config = new DefaultClientConfig();

                        Client client = Client.create(config);
                        WebResource service = client.resource(getBaseURI());
                        System.out.println("service : "+service);
                        System.out.println("URL : "+ getBaseURI().toString());
                        System.out.println("path : "+service.path("rest"));
                        // Get plain text
                        System.out.println("TEXT_PLAIN :
                        // Get XML
                        System.out.println("TEXT_XML :
                        // The HTML
                        System.out.println("TEXT_HTML :
                        JSONObject obj=new JSONObject();
                        obj.put("ToAddr", "");
                        obj.put("FromAddr", "");
                        System.out.println("POST JSON :
            //System.out.println("POST JSON :

                catch(Exception e)

        private static URI getBaseURI() {
                return UriBuilder.fromUri(

help me...

View this message in context:
Sent from the Jersey mailing list archive at