[javax.xml.bind.UnmarshalException: unexpected element

ぐ巨炮叔叔 提交于 2021-01-27 07:42:01

问题


When invkoing a webservice, I get the following exception:

[javax.xml.bind.UnmarshalException: unexpected element (uri:"urn:partner.soap.sforce.com", local:"metadataServerUrl"). Expected elements are <{}sessionId>,<{}sandbox>,<{}userId>,<{}passwordExpired>,<{}metadataServerUrl>,<{}userInfo>,<{}serverUrl>]

The response expected is actually an object called LoginResult. But however I see the element names in the exception strace.

The loginResult class is the expected output object from the webservice call. Please suggest how to fix this.

@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement(name = "result")

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;

回答1:


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

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.*;

For More Information

You can find out more information about JAXB and namespace qualification on my blog:

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html



回答2:


As @Blaise Doughan mentioned already, you need to make sure you've package-info file and that too in correct package.

Did you make sure that line package com.company.project in package-info.java matches with the package structure of your LoginResult class?

Also, print the XML response of web-service before unmarshalling it and make sure the element has actually the namespace associated.




回答3:


in my case issue was due to parallel processes using marshaller and unmrashaller which are set and get by JAXBUtil which is single ton. when I made marshaller and unmarshaller synchronized this inconsistent issue is resolved



来源:https://stackoverflow.com/questions/23253314/javax-xml-bind-unmarshalexception-unexpected-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!