使用 Resteasy 映射 xml 属性
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我他解决问题。
每个元素都需要使用名称空间,并且所有标签都区分大小写!
I'he resove the problem.
It's needed to use namespace for each element and all tags are case sensitive!