1

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   "}}

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: プロローグでコンテンツが許可されていません。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.

4

1 に答える 1

2

注: 私はEclipseLink JAXB(MOXy)のリーダーであり、JAXB(JSR-222)エキスパートグループのメンバーです。

JAXB(JSR-222)仕様は、JSONバインディングをカバーしていません。JAXB注釈付きモデルがJAX-RS実装で使用される場合、発生しているJAXB仕様を超える処理があります。これが、標準のJAXB APIを使用してJSONメッセージを処理しようとすると、XMLパーサー例外が発生する理由です。

デモ

EclipseLink MOXyは、JSONバインディングのネイティブサポートを提供するJAXB実装です(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 "
   }
}
于 2012-11-30T20:32:59.277 に答える