2011-12-11 79 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应用程序)。