Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB what should be returned from `beforeMarshal(Marshaller)` method?

Tags:

java

jaxb

First of all, I'm not talking about Marshaller#Listener. I'm talking about those class defined event callbacks.

Can anybody tell me what should be returned from boolean beforeMarshal(Marshaller) method?

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

I mean, anyway, to use this method for converting JPA's Long @Id to JAXB's String @XmlID with JAXB-RI and without MOXy.

[edited] a void version seems working though. is this just an documentation problem?

like image 390
Jin Kwon Avatar asked Mar 09 '12 06:03

Jin Kwon


1 Answers

Short Answer

The boolean return type is a documentation error. The return type should be void.

Long Answer

I mean, anyway, to use this method for converting JPA's Long @Id to JAXB's String @XmlID

You could use EclipseLink JAXB (MOXy) as it does not have the restriction that a field/property annotated with @XmlID be of type String.

with JAXB-RI and without MOXy.

You could use an XmlAdapter to map support your use case:

IDAdapter

This XmlAdapter converts the Long value to a String value to meet the requirements of the @XmlID annotation.

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

The @XmlJavaTypeAdapter annotation is used to specify the 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;

}

A

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>
like image 126
bdoughan Avatar answered Oct 21 '22 15:10

bdoughan