Jersey - 从 XML 字符串填充 java beans

发布于 2024-11-15 17:31:57 字数 517 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

娇柔作态 2024-11-22 17:31:57

有很多方法可以做到这一点。如果您的 CustomerType 类是 JAXB 注释的(@XmlRootElement 或其他),您可以简单地使用通过 JAXBContext< 构造的 Unmarshaller 。 /code> (您之前已使用包初始化过),如下所示:

CustomerType customerType = (CustomerType) jaxbContext.createUnmarshaller()
        .unmarshal( new StringReader(yourString) );

There are many ways to do that. If your CustomerType class is JAXB-annotated (@XmlRootElement or whatever), you can simply use an Unmarshaller constructed via a JAXBContext (that you have previously initialized with your packages) like this:

CustomerType customerType = (CustomerType) jaxbContext.createUnmarshaller()
        .unmarshal( new StringReader(yourString) );
清醇 2024-11-22 17:31:57

Jersey 提供了日志过滤器。 这篇文章回答了您的问题。

Jersey provides a logging filter. This post answers your question.

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