2012-11-30 31 views
1

Customer.javajavax.xml.bind.UnmarshalException - 与链接除外:[org.xml.sax.SAXParseException:内容未在序言中不允许的。]

@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":"[email protected]","firstName":"first1     ","gender":"F","lastName":"last1     ","midInit":"K","phone":"4082229871  ","state":"CA","streetAddress":"streetAddress1","title":"S ","zipCode":"zipCode "}} 

javax.xml.bind.UnmarshalException - 与链接的异常: : 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl [org.xml.sax.SAXParseException内容不允许在序言。]。 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.bi nd.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194) 在com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.getCustomerByCustNo(CustomerRemoteAgent.java:151) 在com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent。执行(CustomerRemoteAgent.java:311) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:368) 原因:org.xml.sax.SAXParseException:在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 ) 在org.apache.xerces.impl.XMLErrorReporter.reportError(来源不明) 在org.apache.xerces.impl.XMLScanner.reportFatalError(来源不明) 在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。解析(未知来源)(未知源) at org.apache.xerces.jaxp.SAXParserImpl $ JAXPSAXParser.parse(Unknown Source) 来源不明) 在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) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:313) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main (CustomerRemoteAgent.java:368)

+0

'new String(byte [])'几乎肯定是错的 - 从字节数组构造字符串时,您应该始终指定字符编码。或者,在这种情况下,将'byte []'包装在'ByteArrayInputStream'中,然后解组,而不是使用字符串。 –

回答

2

备注:我是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使用您的域模型的例子

要使用莫西为您的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 /输出

{ 
    "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 " 
    } 
} 
相关问题