使用 CXF DOSGI 从 RESTful 服务返回 JSON
我有一个简单的服务,它使用 JAX-RS 注释进行注释,并包含 @Produces("application/json")
注释。我在注册服务时设置了以下属性(我正在使用 DS,但这并不重要):
service.exported.interfaces -> *
service.exported.configs -> org.apache.cxf.rs
org.apache.cxf.rs.address -> myURI
当我运行应用程序时,我可以点击 URL,但我的浏览器返回:
No message body writer has been found for response class MyClass.
我的 OSGi 控制台显示:
Jan 11, 2012 2:29:48 PM org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor writeResponseErrorMessage
WARNING: No message body writer has been found for response class MyClass.
我读了 < a href="http://cxf.apache.org/distributed-osgi-reference.html#DistributedOSGiReference-ServiceProviderpropertiesForConfiguringRESTfulJAXRSbasedendpointsandconsumers" rel="noreferrer">文档 并认为我可能需要注册一个 JSON 提供程序。在 5 月份的 Activator 中我添加了:
bundleContext.registerService(new String[] { "javax.ws.rs.ext.MessageBodyReader",
"javax.ws.rs.ext.MessageBodyWriter" },
new org.apache.cxf.jaxrs.provider.JSONProvider(), null);
但这没有任何区别。
如何解决“未找到响应类 MyClass 的消息正文编写器”的问题。错误消息?
I have a simple service which is annotated with JAX-RS annotations and includes the @Produces("application/json")
annotation. I have set up the following properties when I register the service (I am using DS but that shouldn't matter):
service.exported.interfaces -> *
service.exported.configs -> org.apache.cxf.rs
org.apache.cxf.rs.address -> myURI
When I run my application I can hit the URL, but my browser returns:
No message body writer has been found for response class MyClass.
My OSGi console displays:
Jan 11, 2012 2:29:48 PM org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor writeResponseErrorMessage
WARNING: No message body writer has been found for response class MyClass.
I read the documentation and thought maybe I needed to register a JSON provider. In may Activator I added:
bundleContext.registerService(new String[] { "javax.ws.rs.ext.MessageBodyReader",
"javax.ws.rs.ext.MessageBodyWriter" },
new org.apache.cxf.jaxrs.provider.JSONProvider(), null);
but this has not made any difference.
How do I fix the "No message body writer has been found for response class MyClass." error message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有消息正文编写器意味着您的 json 提供程序不了解如何将您返回的类编组为 JSON。如果您使用默认的 JSONProvider,那么您使用的是 Jackson,它使用 JAXB 注释。换句话说,您返回的类应该在类级别具有 @XmlRootElement 注释。
No message body writer means that your json provider does not understand how to marshal your class that you returned into JSON. If you are using the default JSONProvider, then you are using Jackson, which uses JAXB annotations. In other words, the class that you return should have a @XmlRootElement annotation on the class level.