Jax-RS无法返回json数据
我试图从函数返回 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;
}
}
谁能深入了解这里可能出了什么问题?
I'm trying to return JSON from a function and it's throwing an error about serialization.
The error is:
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) )
I suppose I need to do something with serialization to be sure it returns properly but I'm not sure what.
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;
}
}
Can anyone offer insight into what might be going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您编写的代码是正确的,但主机框架支持的(序列化)Provider 集并未标准化;也许您正在使用的一个没有为
JSONArray
->JSON 注册任何内容?看起来确实是这样。下面是一个示例提供程序:在 JAX-RS 的某些框架实现中,只需将该类构建在类路径上就足够了。在其他(特别是 Apache CXF,也许其他)中,您还需要手动注册它(因为您可以在同一个 Web 应用程序中为不同的服务使用不同的序列化策略;我发现这很有用,但我正在编写一个非常复杂的 Web 应用程序)。
While the code you've written is correct, the set of (serialization) Providers supported by the host framework is not standardized; perhaps you are using one that doesn't have anything registered for
JSONArray
->JSON? It certainly looks like that. Here's a sample provider:In some framework implementations of JAX-RS, merely having that class built on your classpath is enough. In others (notably Apache CXF, maybe others) you need to also register it manually (because you can have different serialization strategies for different services within the same webapp; I've found this useful, but I was writing a very sophisticated webapp).
我看不出代码有任何问题 - 如果我将其以默认设置放入我的示例 Jersey 应用程序中,它就可以开箱即用。如何配置 JSONConfiguration?
您可以将 getHtml() 的返回类型更改为 String 并返回 myData.toString()。不需要隐式序列化。
I cannot see anything wrong with the code - it works out of the box if I put it in my sample Jersey app with default settings. How do you configure your JSONConfiguration?
You could change return type of getHtml() to String and return myData.toString(). No Implicit serialization would be required.