java javax.xml.bind.UnmarshalException - 带有链接异常:[org.xml.sax.SAXParseException:序言中不允许内容。]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13652303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: Content is not allowed in prolog.]
提问by beetri
Customer.java
客户.java
@Entity
@Table(name = "CUSTOMER", uniqueConstraints =
{
@UniqueConstraint(columnNames =
{
"CUSTNO"
})
})
@XmlRootElement
public class Customer
implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id @Column(name = "CUSTNO", length = 10, nullable = false) private String custNo;
@Column(name = "TITLE", length = 20, nullable = false) private String title;
@Column(name = "FIRSTNAME", length = 20, nullable = false) private String
firstName;
@Column(name = "MIDINIT", length = 1, nullable = true) private String
midInit;
@Column(name = "LASTNAME", length = 1, nullable = false) private String
lastName;
@Column(name = "EMAIL", length = 50, nullable = false) private String
email;
@Column(name = "PHONE", length = 16, nullable = true) private String
phone;
@Column(name = "GENDER", length = 1, nullable = true) private String
gender;
@Column(name = "STREETADDRESS", length = 50, nullable = true) private String
streetAddress;
@Column(name = "CITY", length = 20, nullable = true) private String
city;
@Column(name = "STATE", length = 2, nullable = true) private String
state;
@Column(name = "ZIPCODE", length = 10, nullable = true) private String
zipCode;
@Column(name = "COMPANYNAME", length = 25, nullable = true) private String
companyName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer") private Set<ServiceRequest>
requests;
public Customer()
{
}
...... getters/setters....
Client code:
客户端代码:
byte[] getCustomerResponse = (byte[])
RestClientUtil.sendGetMethod(urlGetCustomer + URLEncoder.encode(custNo, "UTF-8"));
Unmarshaller unmarshaller = RestClientUtil.getUnmarshaller(Customer.class);
StringReader reader = new StringReader(new String(getCustomerResponse));
Customer customer = (Customer) unmarshaller.unmarshal(reader);
I see the output as:
我看到输出为:
found customer :{"customer":{"city":"city1 ","companyName":"companyName1 ","custNo":"RCN1","email":"[email protected]","firstName":"first1 ","gender":"F","lastName":"last1 ","midInit":"K","phone":"4082229871 ","state":"CA","streetAddress":"streetAddress1","title":"S ","zipCode":"zipCode "}}
javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: Content is not allowed in prolog.] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:526) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:223) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.getCustomerByCustNo(CustomerRemoteAgent.java:151) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:311) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:368) Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:217) ... 6 more java.lang.NullPointerException at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.submitRequest(CustomerRemoteAgent.java:167) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:313) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:368)
getCustomerByCustNo(CustomerRemoteAgent.java:151) 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:311) 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgents.CustomerRemoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:311)引起:org.xml.sax.SAXParseException:序言中不允许内容。在 org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) 在 org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source) 在 org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) 在 org org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source) at org .apache.xerces。
回答by bdoughan
Note:I'm the EclipseLink JAXB (MOXy)lead and a member of the JAXB (JSR-222)expert group.
注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222)专家组的成员。
The JAXB (JSR-222) specification does not cover JSON binding. When JAXB annotated models are used with JAX-RS implementations there is processing beyond the JAXB spec that is occuring. This is why when you try to use the standard JAXB APIs to process the JSON message you get an XML parser exception.
JAXB (JSR-222) 规范不包括 JSON 绑定。当 JAXB 注释模型与 JAX-RS 实现一起使用时,会发生超出 JAXB 规范的处理。这就是为什么当您尝试使用标准 JAXB API 来处理 JSON 消息时,您会收到 XML 解析器异常的原因。
Demo
演示
EclipseLink MOXy is a JAXB implementation that offers native support for JSON-binding (see: http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html). Below is an example using your domain model as posted in your question (with accessors added)
EclipseLink MOXy 是一个 JAXB 实现,它为 JSON 绑定提供本机支持(请参阅:http: //blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html)。以下是使用您在问题中发布的域模型的示例(添加了访问器)
package forum13652303;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File json = new File("src/forum13652303/input.json");
Customer customer = (Customer) unmarshaller.unmarshal(json);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
}
}
jaxb.properties
jaxb.properties
To use MOXy as your JAXB provider you need to add a file called jaxb.properties
in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):
要将 MOXy 用作您的 JAXB 提供程序,您需要添加一个jaxb.properties
在与域模型相同的包中调用的文件,其中包含以下条目(请参阅:http: //blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as -your.html):
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
input.json/Output
输入.json/输出
{
"customer" : {
"city" : "city1 ",
"companyName" : "companyName1 ",
"custNo" : "RCN1",
"email" : "[email protected]",
"firstName" : "first1 ",
"gender" : "F",
"lastName" : "last1 ",
"midInit" : "K",
"phone" : "4082229871 ",
"state" : "CA",
"streetAddress" : "streetAddress1",
"title" : "S ",
"zipCode" : "zipCode "
}
}