没有消息体编写者发现:JSON:Apache CXF:RestFul Webservices [英] No message body writer found : JSON : Apache CXF : RestFul Webservices
问题描述
我正在使用Apache CXF来创建一个简单的restful应用程序。我有一个客户端类,它将一个JSON对象发布到服务器,服务器在一些操作后返回一个JSON。但是当我执行代码时我得到了
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."
我的客户代码:
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();
}
}
}
请帮忙。
推荐答案
我很久以前就找到了这个,只是为未来的用户发布。实际上,apache cxf(2.5.2)不支持像org.codehaus.jettison.JSONObject这样的原始JSON对象。为了在请求和响应中使用JSON,我使用了pojos(简单的getter和setter with JAXB annotations)和apache cxf json provider,即org.apache.cxf.jaxrs.provider.JSONProvider。以下是我的配置:
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>
这篇关于没有消息体编写者发现:JSON:Apache CXF:RestFul Webservices的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!