Jax-RS无法返回json数据

发布于 2024-12-20 13:49:17 字数 1354 浏览 0 评论 0原文

我试图从函数返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-12-27 13:49:17

虽然您编写的代码是正确的,但主机框架支持的(序列化)Provider 集并未标准化;也许您正在使用的一个没有为 JSONArray->JSON 注册任何内容?看起来确实是这样。下面是一个示例提供程序:

@Provider
public class JSONArraySerializer implements MessageBodyWriter<JSONArray> {
    @Override
    public boolean isWriteable(Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        // Applicability condition: writing JSONArray to application/json
        if (JSONArray.class.isAssignableFrom(type))
            return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
        return false;
    }
    @Override
    public long getSize(JSONArray array, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        return -1; // Can't be bothered to calculate
    }
    @Override
    public void writeTo(JSONArray array, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, Object> httpHeaders,
            OutputStream entityStream) throws IOException,
            WebApplicationException {
        try {
            // Strictly should say encoding here; don't know right value...
            array.write(new OutputStreamWriter(entityStream));
        } catch (JSONException e) {
            throw new WebApplicationException(e);
        }
    }
}

在 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:

@Provider
public class JSONArraySerializer implements MessageBodyWriter<JSONArray> {
    @Override
    public boolean isWriteable(Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        // Applicability condition: writing JSONArray to application/json
        if (JSONArray.class.isAssignableFrom(type))
            return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
        return false;
    }
    @Override
    public long getSize(JSONArray array, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        return -1; // Can't be bothered to calculate
    }
    @Override
    public void writeTo(JSONArray array, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, Object> httpHeaders,
            OutputStream entityStream) throws IOException,
            WebApplicationException {
        try {
            // Strictly should say encoding here; don't know right value...
            array.write(new OutputStreamWriter(entityStream));
        } catch (JSONException e) {
            throw new WebApplicationException(e);
        }
    }
}

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).

删除会话 2024-12-27 13:49:17

我看不出代码有任何问题 - 如果我将其以默认设置放入我的示例 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文