如何创建 MessageBodyWriter 以在 RestEasy 中以 HTML 形式返回自定义对象?

发布于 2024-12-05 02:48:19 字数 1777 浏览 2 评论 0原文

我在 Tomcat 中将 RestEasy 与 Spring 一起使用。我有一个简单的控制器方法,我想通过 Ajax(使用 JSON 或 XML 响应)和标准浏览器请求(使用 HTML 作为响应)来使用它。当我使用简单的返回数据类型(如字符串)但我需要返回自定义对象时,它可以工作:

@POST
@Path("fooBar")
public RequestResult fooBar()
{
    return new RequestResult();
}

这是 RequestResult 对象(只是用于演示的虚拟实现):

@XmlRootElement(name = "result")
public final class RequestResult
{
    @XmlAttribute
    public boolean getWhatever()
    {
        return "whatever";
    }
}

当以 JSON 或 XML 形式请求它时,但以 HTML 形式请求它时,它可以工作我收到错误消息无法找到媒体类型的 JAXBContextFinder:text/html。很明显,它无法工作,因为 RestEasy 不知道如何将此对象转换为 HTML。所以我添加了这个测试 MessageBodyWriter:

@Provider
@Produces("text/html")
public class ResultProvider implements MessageBodyWriter<RequestResult>
{
    @Override
    public boolean isWriteable(final Class<?> type, final Type genericType,
        final Annotation[] annotations, final MediaType mediaType)
    {
        return true;
    }

    @Override
    public long getSize(final RequestResult t, final Class<?> type, final Type genericType,
        final Annotation[] annotations, final MediaType mediaType)
    {
        return 4;
    }

    @Override
    public void writeTo(final RequestResult t, final Class<?> type, final Type genericType,
        final Annotation[] annotations, final MediaType mediaType,
        final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
        throws IOException, WebApplicationException
    {
        final PrintWriter writer = new PrintWriter(entityStream);
        writer.println("Test");
    }
}

但这并没有改变任何东西。从未调用过该提供程序的任何方法。我不确定是否必须在某个地方注册它。所有其他类都是通过类路径扫描自动找到的,所以我想这也发生在提供者身上。

我很确定我做错了什么或者我忘记了什么。有什么提示吗?

I'm using RestEasy together with Spring in Tomcat. I have a simple controller method which I want to use via Ajax (with JSON or XML response) and via a standard browser request (Using HTML as a response). It works when I use simple return data types like a string but I need to return a custom object:

@POST
@Path("fooBar")
public RequestResult fooBar()
{
    return new RequestResult();
}

This is the RequestResult object (Just a dummy implementation for demonstration):

@XmlRootElement(name = "result")
public final class RequestResult
{
    @XmlAttribute
    public boolean getWhatever()
    {
        return "whatever";
    }
}

It works when requesting it as JSON or XML but when requesting it as HTML I get the error message Could not find JAXBContextFinder for media type: text/html. It's clear that it can't work because RestEasy doesn't know how to convert this object to HTML. So I added this test MessageBodyWriter:

@Provider
@Produces("text/html")
public class ResultProvider implements MessageBodyWriter<RequestResult>
{
    @Override
    public boolean isWriteable(final Class<?> type, final Type genericType,
        final Annotation[] annotations, final MediaType mediaType)
    {
        return true;
    }

    @Override
    public long getSize(final RequestResult t, final Class<?> type, final Type genericType,
        final Annotation[] annotations, final MediaType mediaType)
    {
        return 4;
    }

    @Override
    public void writeTo(final RequestResult t, final Class<?> type, final Type genericType,
        final Annotation[] annotations, final MediaType mediaType,
        final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
        throws IOException, WebApplicationException
    {
        final PrintWriter writer = new PrintWriter(entityStream);
        writer.println("Test");
    }
}

But this doesn't change anything. No method of this provider is ever called. I'm not sure if I have to register it somewhere. All other classes are found automatically by classpath scanning so I guess this also happens for providers.

I'm pretty sure I made something wrong or I forgot something. Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你另情深 2024-12-12 02:48:20

尝试在您的 fooBar() 方法中添加包含 "text/html"@Produces 注释(我添加了 JSON 和 XML 因为听起来您想要全部三个)。当我这样做时,您的 ResultProvider 被调用。让我知道这是否适合您!

@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_HTML })
@Path("fooBar")
public RequestResult fooBar()
{
    return new RequestResult();
}

Try adding adding a @Produces annotation that includes "text/html" to your fooBar() method (I included JSON and XML because it sounded like you wanted all three). When I did that, your ResultProvider was called. Let me know if that works for you!

@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_HTML })
@Path("fooBar")
public RequestResult fooBar()
{
    return new RequestResult();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文