如何创建 MessageBodyWriter 以在 RestEasy 中以 HTML 形式返回自定义对象?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在您的
fooBar()
方法中添加包含"text/html"
的@Produces
注释(我添加了JSON 和
XML
因为听起来您想要全部三个)。当我这样做时,您的ResultProvider
被调用。让我知道这是否适合您!Try adding adding a
@Produces
annotation that includes"text/html"
to yourfooBar()
method (I includedJSON
andXML
because it sounded like you wanted all three). When I did that, yourResultProvider
was called. Let me know if that works for you!