No message body writer found : JSON : Apache CXF : RestFul Webservices
Asked Answered
L

6

5

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.

Laurenlaurena answered 13/2, 2012 at 5:45 Comment(0)
L
5

I had found this long back ago, just posting it for future users. Actually apache cxf(2.5.2) doesn't support raw JSON objects like org.codehaus.jettison.JSONObject. For using JSON in requests and responses, I used pojos(simply getters and setters with JAXB annotations) and apache cxf json provider i.e. org.apache.cxf.jaxrs.provider.JSONProvider. Following is my configuration :

<jaxrs:providers>
    <bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
        <property name="dropRootElement" value="true" />
        <property name="supportUnwrapped" value="true" />
    </bean>
</jaxrs:providers>
Laurenlaurena answered 24/4, 2012 at 7:36 Comment(0)
D
10

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>
Demmer answered 20/12, 2012 at 22:44 Comment(2)
yea there were some package changes after 2.6 releaseLaurenlaurena
This Worked.. Thanks. I also need to include jettison dependency in mavenLatrinalatrine
L
5

I had found this long back ago, just posting it for future users. Actually apache cxf(2.5.2) doesn't support raw JSON objects like org.codehaus.jettison.JSONObject. For using JSON in requests and responses, I used pojos(simply getters and setters with JAXB annotations) and apache cxf json provider i.e. org.apache.cxf.jaxrs.provider.JSONProvider. Following is my configuration :

<jaxrs:providers>
    <bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
        <property name="dropRootElement" value="true" />
        <property name="supportUnwrapped" value="true" />
    </bean>
</jaxrs:providers>
Laurenlaurena answered 24/4, 2012 at 7:36 Comment(0)
B
3

I also had this problem, any of above solutions didn't work for me. Inorder to resolve this issue you need to do a couple of things which I will explain.

In cxf-servlet.xml(server configuration) one of json providers needs to be provided.

         <jaxrs:server id="categoryRESTService" address="/api">
            <jaxrs:features>
                <cxf:logging/>
            </jaxrs:features>
             <jaxrs:serviceBeans>
                <ref bean="categoryService" />
            </jaxrs:serviceBeans>
             <jaxrs:providers>
                 <ref bean="jsonProvider"/>
             </jaxrs:providers>
          </jaxrs:server> 
          <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>

In the client, it can be configured using spring or source code itself. Here is the how it is done in source code itself.

 WebClient client = WebClient.create("http://localhost:9763/services/api/", Collections.singletonList(new JacksonJsonProvider()))
                .path("categoryservice/category/001").accept(MediaType.APPLICATION_JSON_TYPE);
 Category category = client.get(Category.class);

So here I have a class called Category, that is why I have done in that way. So now I hope you could understand how to resolve this error. One more thing needs to tell about URLs. When you create a WebClient instance need to give where is the service is deployed.And add a relative path to the desired endpoint using path as shown above example code.

Boldfaced answered 26/10, 2015 at 9:59 Comment(0)
F
0

I'm not sure from your writing whether the error is being generated by the client or the REST service. Also, does your REST service ever successfully return JSON (and it's just this client with the trouble) or is it always failing (indicating the problem is with the service)? If the service is the problem, point #10 here: http://www.jroller.com/gmazza/entry/jersey_samples_on_cxf notes that you'll need to add in a JSON provider and dependency as shown, else it won't know how to process JSON. That might be the issue.

Factional answered 13/2, 2012 at 19:17 Comment(1)
The problem is whenever i execute the code i get the "No message body writer for org.codehaus.jettison.json" exception. i am no expert programmer and this is my first time with restful services. Does cxf support json object in a way i have used in my code? I have used jersey framework it allowed this type of codes without any errors and also i didnt mentioned any providers in my web.xml. Please note that i would prefer a non spring solution.Laurenlaurena
K
0

A different approach: I found that when I changed my "Accept" request header from "application/json" to "/" then I no longer received the "No message body writer found" error message.

Klusek answered 16/1, 2014 at 17:42 Comment(0)
S
0

For me, this issue came up as a result of converting to Jackson 2.x. and still wanting to use CXF with JaxRS.

The above suggestions from MikeLand and Sikorski worked for me.

The migration guide actually discusses this

CXF Migration Guide

Scroll down to "Dependency Changes."

I'm just adding the required dependencies for your Maven pom.xml file:

    <!--  for JSON provider -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <!--  for extension providers -->
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.2</version>
    </dependency>   

This is one of those deals that took way longer than it should have to resolve. Hopefully this willl help someone.

Shae answered 28/5, 2015 at 20:49 Comment(1)
More details here: issues.apache.org/jira/browse/CXF-4996 The issue was repaired in certain versions of CXF. This is now working for me.Shae

© 2022 - 2024 — McMap. All rights reserved.