Message body writer not found: JSON: Apache CXF: RestFul Webservices

I use Apache CXF to create a simple soothing application. I have a client class that sends a JSON object to the server, and the server returns some JSON after some manipulations. 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.

+5
source share
6 answers

, . apache cxf (2.5.2) JSON, org.codehaus.jettison.JSONObject. JSON pojos ( JAXB) apache cxf json provider i.e. org.apache.cxf.jaxrs.provider.JSONProvider. :

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

, CXF 2.7. org.apache.cxf.jaxrs.provider.JSONProvider org.apache.cxf.jaxrs.provider.json.JSONProvider

, jaxrs:

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

, . , , .

cxf-servlet.xml( ) json.

         <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"/>

spring . .

 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);

, , . , , . URL-. WebClient, , , . , , , .

+3

, , REST. , - REST - JSON ( ), ( )? , # 10: http://www.jroller.com/gmazza/entry/jersey_samples_on_cxf , JSON , , , JSON. .

0

: , , "" "application/json" "/", " ".

0
source

For me, this problem arose as a result of conversion to Jackson 2.x. and still wants to use CXF with JaxRS.

The above suggestions from MikeLand and Sikorski worked for me.

Migration guide really discuss this

CXF Migration Guide

Scroll down to "Change Dependencies."

I just add the necessary 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 went a lot longer than they needed to be resolved. Hope this helps someone.

0
source

All Articles