2011-12-11 78 views
3

我想從函數返回JSON,並且拋出一個有關序列化的錯誤。Jax-RS無法返回JSON數據

的錯誤是:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.codehaus.jettison.json.JSONArray and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)) 

我想我需要做一些系列化,以確保它返回正確的,但我不知道是什麼。

package contentmanagement; 

import javax.ws.rs.core.Context; 
import javax.ws.rs.core.UriInfo; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.PUT; 
import javax.ws.rs.Path; 
import javax.ws.rs.GET; 
import javax.ws.rs.Produces; 
import org.codehaus.jettison.json.JSONArray; 

/** 
* REST Web Service 
*/ 
@Path("signups") 
public class ContentManagement { 

    @Context 
    private UriInfo context; 

    /** Creates a new instance of ContentManagement */ 
    public ContentManagement() { 
    } 

    /** 
    * Retrieves representation of an instance of contentmanagement.ContentManagement 
    * @return an instance of java.lang.String 
    */ 
    @GET @Path("getHtml") 
    @Produces("application/json") 
    public JSONArray getHtml() { 
     JSONArray myData = new JSONArray(); 

     for (int x = 0; x < 12; x++) { 
      myData.put("This is a test entry"+x); 
     } 

     return myData; 
    } 
} 

任何人都可以提供洞察哪裏可能會出錯嗎?

回答

1

我看不出代碼有什麼問題 - 如果我將它放在示例Jersey應用程序的默認設置中,它就可以使用。你如何配置你的JSONConfiguration?

您可以將getHtml()的返回類型更改爲String並返回myData.toString()。不需要隱式序列化。

2

雖然您編寫的代碼是正確的,但主機框架支持的(序列化)提供程序集不是標準化的;也許你正在使用一個沒有註冊JSONArray - > JSON的東西?它當然是這樣的。下面是一個示例提供者:在一些JAX-RS的框架實現中,僅僅在類路徑上構建該類就足夠了。在其他人(尤其是Apache CXF,也許是其他人)中,您還需要手動註冊(因爲您可以在同一個Web應用程序中爲不同的服務提供不同的序列化策略;我發現這很有用,但我正在編寫一個非常複雜的Web應用程序)。