2012-11-30 36 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 " 
    } 
} 
相關問題