No message body writer found : JSON : Apache CXF : RestFul Webservices

Sikorski picture Sikorski · Feb 13, 2012 · Viewed 46.3k times · Source

I am using Apache CXF for making a simple restful application. I have a client class which posts a JSON object to the server and server returns back a JSON after some manipulation. but when i execute the code i get

"org.apache.cxf.interceptor.Fault: .No message body writer has been found for class:           
 class org.codehaus.jettison.json.JSONObject, ContentType : application/json."

My client code :

public class Client {
public static void main(String[] args) {

    try{

        URI uri =  new URI("http://localhost:8022/RestDemo");

        WebClient client = WebClient.create(uri);

        String ret = client.path("rest").path("server").path("welcome").accept(MediaType.TEXT_PLAIN).get(String.class);

        System.out.println(ret);

        JSONObject json = new JSONObject();
    json.put("name", "ronaldo");
    json = client.path("rest").path("server").path("op").type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(json, JSONObject.class);
    System.out.println(json);
    System.out.println(json.has("reverse")?json.getString("reverse"):"dont have");


    }catch(Exception e){
        System.out.println("e"+e.getLocalizedMessage());
        e.printStackTrace();
    }
}
}

Please help.

Answer

mikeland picture mikeland · Dec 20, 2012

I had the same problem and I am using CXF 2.7. The org.apache.cxf.jaxrs.provider.JSONProvider has been changed to org.apache.cxf.jaxrs.provider.json.JSONProvider

So the jaxrs provider is:

<jaxrs:providers>
    <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
        <property name="dropRootElement" value="true" />
        <property name="supportUnwrapped" value="true" />
    </bean>
</jaxrs:providers>