views:

362

answers:

1

I have a basic JavaBean in my service layer and originally it was not designed for marshalling. This object is used by both JAX-WS, JAX-RS, and Servlets in other layers of my application. I want to take advantage of a drill down type effect in my REST services so my question is: How do I make one of the fields/properties of the javabean appear in the xml as an HTML Link? Obviously I need to use CData. I cannot modify the original javabean by adding fields, etc. Is there an annotation I can use?

If I have in my class:

...
String data;
...

how do I make that(in xml):

<data><![CDATA[<a href="somearbitrarylink">ValueOfData</a>]]></data>

is this possible with JAXB and Annotations? Maybe xlink?

+2  A: 

I suggest using a type adapter. These are normally used to adapt XML string values into more strongly-typed values like timestamps, but you can also use them to adapt strings to strings.

First, create a subclass of javax.xml.bind.annotation.adapters.XmlAdapter. This class will have to implement marshal and unmarshal, converting to and from the value of your field, and the HTML fragment in the XML.

Once you have that, you can annotate your field with

@XmlJavaTypeAdapter(MyAdapter.class)

And that should be it.

skaffman