问题描述
调用网络服务时,出现以下异常:
When invkoing a webservice, I get the following exception:
[javax.xml.bind.UnmarshalException:意外元素(uri:"urn:partner.soap.sforce.com",本地:"metadataServerUrl").期望的元素是< {} sessionId>,< {} sandbox>,< {} userId>,< {} passwordExpired>,< {} metadataServerUrl>,< {} userInfo>,< {} serverUrl >]
[javax.xml.bind.UnmarshalException: unexpected element (uri:"urn:partner.soap.sforce.com", local:"metadataServerUrl"). Expected elements are <{}sessionId>,<{}sandbox>,<{}userId>,<{}passwordExpired>,<{}metadataServerUrl>,<{}userInfo>,<{}serverUrl>]
预期的响应实际上是一个名为LoginResult的对象.但是,但是我在异常strace中看到了元素名称.
The response expected is actually an object called LoginResult. But however I see the element names in the exception strace.
loginResult类是来自webservice调用的预期输出对象.请提出解决方法.
The loginResult class is the expected output object from the webservice call. Please suggest how to fix this.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "result")
公共类LoginResult {
public class LoginResult {
@XmlElement(name = "metadataServerUrl")
protected String metadataServerUrl;
@XmlElement(name = "passwordExpired")
protected boolean passwordExpired;
@XmlElement(name = "sandbox")
protected boolean sandbox;
@XmlElement(name = "serverUrl")
protected String serverUrl;
@XmlElement(name = "sessionId")
protected String sessionId;`
@XmlElement(name = "userId")
protected String userId;
@XmlElement(name = "userInfo")
protected GetUserInfoResult userInfo;
推荐答案
您需要利用程序包级别的@XmlSchema
批注(在名为package-info
的特定类上指定名称空间限定.如果您已经拥有一个程序包, info.java文件确保已在编译它.
You need to leverage the package level @XmlSchema
annotation (on a specical class called package-info
to specify the namespace qualification. If you already have a package-info.java file make sure it is being compiled.
package-info.java
下面是package-info.java
文件的完整内容.您需要将包从example
更改为包含您要对其应用名称空间限定的域模型的包.
Below is the complete contents of the package-info.java
file. You will need to change the package from example
to the package that contains your domain model to which you want the namespace qualification applied.
@XmlSchema(
namespace = "urn:partner.soap.sforce.com",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.*;
更多信息
您可以在我的博客上找到有关JAXB和名称空间限定的更多信息:
You can find out more information about JAXB and namespace qualification on my blog:
这篇关于[javax.xml.bind.UnmarshalException:意外元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!