Jersey - 从 XML 字符串填充 java beans
我有一个 Jersey 客户端,它成功调用 REST 服务并根据以下代码填充关联的 Java Bean CustomerType
:
WebResource service = client.resource(SECURE_BASE_URL);.
CustomerType cust = service.path("customer").path("23210")
.accept(MediaType.TEXT_XML).get(CustomerType.class);
我想要的是调用该服务
service.path("customer").path("23210").accept(MediaType.TEXT_XML).get(String.class);
来获取 XML 字符串,然后将XML 到 CustomerType
bean。我想以这种方式进行日志记录并帮助设计系统。有没有办法将 XML 转换为 bean?
I have a Jersey client that is successfully calling a REST service and populating the associated Java Beans CustomerType
based on this code:
WebResource service = client.resource(SECURE_BASE_URL);.
CustomerType cust = service.path("customer").path("23210")
.accept(MediaType.TEXT_XML).get(CustomerType.class);
What I would like is to call the service with
service.path("customer").path("23210").accept(MediaType.TEXT_XML).get(String.class);
to get the XML string and then convert the XML to the CustomerType
bean. I would like to do it this way for logging and to help with designing the system. Is there a ways to convert the XML to the bean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以做到这一点。如果您的
CustomerType
类是 JAXB 注释的(@XmlRootElement
或其他),您可以简单地使用通过JAXBContext< 构造的
Unmarshaller
。 /code> (您之前已使用包初始化过),如下所示:There are many ways to do that. If your
CustomerType
class is JAXB-annotated (@XmlRootElement
or whatever), you can simply use anUnmarshaller
constructed via aJAXBContext
(that you have previously initialized with your packages) like this:Jersey 提供了日志过滤器。 这篇文章回答了您的问题。
Jersey provides a logging filter. This post answers your question.