使用 Resteasy 映射 xml 属性

发布于 2024-09-24 06:37:47 字数 2365 浏览 3 评论 0原文

我正在使用 Resteasy,但遇到了一些问题。

远程服务器上有一些服务。例如,当我转到网址

http://some.site.com/Login/my_login /my_passowrd”服务返回我 xml:

< LoginToken>
  < success>true< /success>
  < token>23324473-897b-408b-b096-bf2cddad33< /token>
< /LoginToken>

我已经编写了用于映射的类:

@XmlRootElement(name = "LoginToken")
public class LoginToken {

  @XmlElement(name="success")
  private String success;

  @XmlElement(name="token")
  private String token;

  public String getSuccess() {
      return success;
    }

  public String getToken() {
      return token;
    }
}

..以及另一个用于测试的代码:

final String loginUrl "http://some.site.com/Login/{login}/{password}";
ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();
RegisterBuiltin.register(instance);

ClientRequestFactory client = new ClientRequestFactory();

LoginToken ct = client.get(loginUrl, LoginToken.class, "user", "123456");

System.out.println(ct.getSuccess() + " " + ct.getToken());

运行此示例后,我收到结果:

真23324473-897b-408b-b096-bf2cddad33

但服务器可以返回另一个 xml:

< LoginToken xmlns="some.site.com/login/api/domain" mlns:i="http://www.w3.org/2001/XMLSchema-instance">
  < success>true< /success>
  < token>23324473-897b-408b-b096-bf2cddad33< /token>
< /LoginToken>

并且我有解组错误:

Exception in thread "main" org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.UnmarshalException: unexpected element (uri:"some.site.com/login/api/domain", local:"LoginToken"). Expected elements are <{}LoginToken>
        at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.readFrom(AbstractJAXBProvider.java:86)
        at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)
        at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:46)
        at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)
...

问题:如何使用 Resteasy 映射 xml 命名空间?请帮忙。谢谢。

I'm using resteasy and have some problem.

There is some service on remote server. For example, when I go to url

"http://some.site.com/Login/my_login/my_passowrd" service returns me xml:

< LoginToken>
  < success>true< /success>
  < token>23324473-897b-408b-b096-bf2cddad33< /token>
< /LoginToken>

I've wrote class for mapping:

@XmlRootElement(name = "LoginToken")
public class LoginToken {

  @XmlElement(name="success")
  private String success;

  @XmlElement(name="token")
  private String token;

  public String getSuccess() {
      return success;
    }

  public String getToken() {
      return token;
    }
}

..and another code for testing:

final String loginUrl "http://some.site.com/Login/{login}/{password}";
ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();
RegisterBuiltin.register(instance);

ClientRequestFactory client = new ClientRequestFactory();

LoginToken ct = client.get(loginUrl, LoginToken.class, "user", "123456");

System.out.println(ct.getSuccess() + " " + ct.getToken());

after running this example I receive result:

true 23324473-897b-408b-b096-bf2cddad33

But server can returns another xml:

< LoginToken xmlns="some.site.com/login/api/domain" mlns:i="http://www.w3.org/2001/XMLSchema-instance">
  < success>true< /success>
  < token>23324473-897b-408b-b096-bf2cddad33< /token>
< /LoginToken>

and i have unmarshall error:

Exception in thread "main" org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.UnmarshalException: unexpected element (uri:"some.site.com/login/api/domain", local:"LoginToken"). Expected elements are <{}LoginToken>
        at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.readFrom(AbstractJAXBProvider.java:86)
        at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)
        at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:46)
        at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)
...

Question: how can I map xml-namespace using resteasy? Please, help. Thanks.

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

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

发布评论

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

评论(1

梦归所梦 2024-10-01 06:37:47

我他解决问题。
每个元素都需要使用名称空间,并且所有标签都区分大小写!

I'he resove the problem.
It's needed to use namespace for each element and all tags are case sensitive!

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