【发布时间】:2012-11-19 02:12:33
【问题描述】:
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.createUnmarshalException(AbstractUnmarshallerImpl.java:315) 在 com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:526) 在 com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:223) 在 com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194) 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.getCustomerByCustNo(CustomerRemoteAgent.java:151) 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:311) 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:368) 原因:org.xml.sax.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.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:217) ... 6 更多 java.lang.NullPointerException 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.submitRequest(CustomerRemoteAgent.java:167) 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:313) 在 com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:368)
【问题讨论】:
-
new String(byte[])几乎肯定是错误的——从字节数组构造字符串时,您应该始终指定字符编码。或者在这种情况下,将byte[]包装在ByteArrayInputStream中并从中解组,而不是使用字符串。
标签: java rest binding jaxb saxparseexception