没有消息正文编写者发现:JSON:Apache CXF:RestFul Webservices

Sik*_*ski 5 java json cxfrs

我正在使用Apache CXF来创建一个简单的restful应用程序.我有一个客户端类,它将一个JSON对象发布到服务器,服务器在一些操作后返回一个JSON.但是当我执行代码时,我得到了

"org.apache.cxf.interceptor.Fault: .No message body writer has been found for class:           
 class org.codehaus.jettison.json.JSONObject, ContentType : application/json."
Run Code Online (Sandbox Code Playgroud)

我的客户代码:

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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

请帮忙.

小智 10

我有同样的问题,我正在使用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>
Run Code Online (Sandbox Code Playgroud)


Sik*_*ski 5

我很早以前就发现了这一点,只是将其发布给以后的用户。实际上,Apache cxf(2.5.2)不支持org.codehaus.jettison.JSONObject之类的原始JSON对象。为了在请求和响应中使用JSON,我使用了pojos(带有JAXB注释的简单getter和setter)和apache cxf json提供程序,即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>
Run Code Online (Sandbox Code Playgroud)