No message body writer has been found for class com.alibaba.fastjson.JSONObject, ContentType: */*

1:当使用 cxf 发布服务时,要求返回值类型为xml,或者json等前端

@Path("/searchProductByText") @GET @Produces({"application/xml", "application/json"}) JSONObject productSearch(@QueryParam("text") String text);

可是 cxf不支持解析  JSONObject 等对象java

进行访问时将会返回    No message body writer has been found for class com.alibaba.fastjson.JSONObject, ContentType: */*json

2:解决方法,定义一个pojo,里面包含 JSONObject  引用。将返回的JSONObject包含在 自定义的 POJO中,使用注解将pojo定义为能被解析为xml形式等。app

    

package com.search.pojo; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.annotation.JsonInclude; import javax.xml.bind.annotation.XmlRootElement; //@JsonInclude(JsonInclude.Include.NON_NULL)//不包含有null值的字段,即字段值为null的转换为json字符串时会被省略
@XmlRootElement(name="MyJSONObject") public class MyJSONObject { JSONObject jsonObject; public JSONObject getJsonObject() { return jsonObject; } public void setJsonObject(JSONObject jsonObject) { this.jsonObject = jsonObject; } }

3:在实现类中,返回对象变为 pojothis

     

MyJSONObject myJSONObject=new MyJSONObject(); myJSONObject.setJsonObject(jsonObject); return myJSONObject;

4:接口 方法 返回 也变为 pojourl

    

@Path("/searchProductByText") @GET @Produces({"application/xml", "application/json"}) MyJSONObject productSearch(@QueryParam("text") String text);

5  能够返回值了。spa

 6:前端接到响应体中的xml,能够转化为jsoncode

  

httpResponse = httpUtil.HttpGet(requestParamMap, url); HttpEntity entity = httpResponse.getEntity(); String s = EntityUtils.toString(entity); //将返回的xml字符串形式转化为json格式
            org.json.JSONObject xmlJSONObj = XML.toJSONObject(s); jsonObject = (JSONObject) JSON.parse(xmlJSONObj.toString());