2012-03-09 5 views
8

Trước hết, tôi không nói về Marshaller#Listener. Tôi đang nói về những lời gọi sự kiện class defined đó.JAXB những gì cần được trả về từ phương thức `beforeMarshal (Marshaller)`?

Ai có thể cho tôi biết cần trả lại gì từ phương thức boolean beforeMarshal(Marshaller)?

/** 
* Where is apidocs for this method? 
* What should I return for this? 
*/ 
boolean beforeMarshal(Marshaller marshaller); 

Ý tôi là, dù sao, sử dụng phương pháp này để chuyển đổi JPA's Long @Id to JAXB's String @XmlIDvới JAXB-RImà không MOXY.

[chỉnh sửa] phiên bản void có vẻ hoạt động. đây có phải là vấn đề về tài liệu không?

Trả lời

7

ngắn trả lời

Các boolean kiểu trả về là một lỗi tài liệu. Loại trả về phải là void.

dài trả lời

Ý tôi là, dù sao, sử dụng phương pháp này để chuyển đổi dài @ id JPA để JAXB của Chuỗi @XmlID

Bạn có thể sử dụng EclipseLink JAXB (MOXy) vì nó không có hạn chế rằng một trường/thuộc tính được chú thích với @XmlID là loại String.

với JAXB-RI và không có MOXy.

Bạn có thể sử dụng một XmlAdapter để lập bản đồ hỗ trợ trường hợp sử dụng của bạn:

IDAdapter

XmlAdapter này chuyển giá trị Long đến một giá trị String để đáp ứng các yêu cầu của @XmlID chú thích.

package forum9629948; 

import javax.xml.bind.DatatypeConverter; 
import javax.xml.bind.annotation.adapters.XmlAdapter; 

public class IDAdapter extends XmlAdapter<String, Long> { 

    @Override 
    public Long unmarshal(String string) throws Exception { 
     return DatatypeConverter.parseLong(string); 
    } 

    @Override 
    public String marshal(Long value) throws Exception { 
     return DatatypeConverter.printLong(value); 
    } 

} 

B

Các @XmlJavaTypeAdapter chú thích được sử dụng để xác định XmlAdapter:

package forum9629948; 

import javax.xml.bind.annotation.*; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class B { 

    @XmlAttribute 
    @XmlID 
    @XmlJavaTypeAdapter(IDAdapter.class) 
    private Long id; 

} 

Một

package forum9629948; 

import javax.xml.bind.annotation.*; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class A { 

    private B b; 
    private C c; 

} 

C

package forum9629948; 

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD)public class C { 

    @XmlAttribute 
    @XmlIDREF 
    private B b; 

} 

Demo

package forum9629948; 

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(A.class); 

     File xml = new File("src/forum9629948/input.xml"); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     A a = (A) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(a, System.out); 
    } 

} 

Input/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<a> 
    <b id="123"/> 
    <c b="123"/> 
</a>