javax.xml.bind.UnmarshalException-带有链接的异常:[org.xml.sax.SAXPa



Customer.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....

客户端代码:

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);

我认为输出为:

found customer :{"customer":{"city":"city1         ","companyName":"companyName1            ","custNo":"RCN1","email":"email1@ge.com","firstName":"first1                   ","gender":"F","lastName":"last1                    ","midInit":"K","phone":"4082229871      ","state":"CA","streetAddress":"streetAddress1","title":"S   ","zipCode":"zipCode   "}}

javax.xml.bind.UnmarshalException-具有链接的异常:[org.xml.sax.SAXParseException:序言中不允许有内容。]位于javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshallException(AbstractUnmarhallerImpl.java:315)在com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshalerImpl.createUnmarshallException(UnmarshallerImpl.java:526)位于com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshalleImpl.unmarshall0(UnmarshallerImpl.java:223)网址:com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshalerImpl.unmarshall(UnmarshallerImpl.java:189)位于javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshall(AbstractUnmarhallerImpl.java:137)位于javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshall(AbstractUnmarhallerImpl.java:194)位于com.ge.dps.iworkRemote.remoteAgents.CustomerRemoteAgent.getCustomerByCustNo(CustomerRemoteAgent.java:151)位于com.ge.dps.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:311)位于com.ge.dps.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:368)由以下原因引起:org.xml.ax.SAXParseException:prolog中不允许有内容。位于org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(未知源)网址:org.apache.xerces.util.ErrorHandlerWrapper.fatalError(未知来源)网址:org.apache.xerces.impl.XMLErrorReporter.reportError(未知来源)网址:org.apache.xerces.impl.XMLErrorReporter.reportError(未知来源)网址:org.apache.xerces.impl.XMLScanner.reportFatalError(未知来源)网址:org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(未知来源)网址:org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(未知来源)网址:org.apache.xerces.parsers.XML11Configuration.parse(未知来源)网址:org.apache.xerces.parsers.XML11Configuration.parse(未知来源)网址:org.apache.xerces.parsers.XMLParser.parse(未知来源)网址:org.apache.xerces.parsers.AbstractSAXParser.parse(未知来源)网址:org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(未知来源)位于com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshalleImpl.unmarshall0(UnmarshallerImpl.java:217)…还有6个java.lang.NullPointerException位于com.ge.dps.iworkRemote.remoteAgents.CustomerRemoteAgent.submitRequest(CustomerRemoteAgent.java:167)位于com.ge.dps.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:313)位于com.ge.dps.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:368)

注意:我是EclipseLink JAXB(MOXy)的负责人,也是

JAXB(JSR-222)JAXB(JSR-222)规范不包括JSON绑定。当JAXB注释模型与JAX-RS实现一起使用时,会出现超出JAXB规范的处理。这就是为什么当您尝试使用标准JAXB API来处理JSON消息时,会出现XML解析器异常。

演示

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

要使用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

{
"customer" : {
"city" : "city1 ",
"companyName" : "companyName1 ",
"custNo" : "RCN1",
"email" : "email1@ge.com",
"firstName" : "first1 ",
"gender" : "F",
"lastName" : "last1 ",
"midInit" : "K",
"phone" : "4082229871 ",
"state" : "CA",
"streetAddress" : "streetAddress1",
"title" : "S ",
"zipCode" : "zipCode "
}
}

相关内容

最新更新