1

I am using JAXB 2.0 for the Application Deveopment which is using RestFul Webservices . Now there is a modification in the request , that is i will be getting another filed/variable in the request XML .

<Root Id="567" att="758" />   

Modified Request will be

 <Root Id="567" att="758" anotherfiledadded ="kiran" />   

My question is , is it possible to automatically append that field (anotherfiledadded)in the UserData class (Without modifying the UserData ??)

The below is my UserData class

@XmlRootElement(name = "Root")
@XmlAccessorType(XmlAccessType.FIELD)

public class UserData {

    @XmlAttribute
    private String Id;

    @XmlAttribute
    private String att;

// getters and setters 
Pawan
  • 31,545
  • 102
  • 256
  • 434

2 Answers2

1

You can try adding the field at runtime with javassist. But... It looks like you would also require to add the Annotation @XmlAttribute and I don't know if javassist allows you to add annotations... Anyways give it a try.

See: Javassist Add

davidmontoyago
  • 1,834
  • 14
  • 18
0

You could use XSLT to apply an attribute into your XML document. All of the classes below are available in the JDK/JRE since Java SE 6.

JAXBContext jc = JAXBContext.newInstance(UserData.class); 
JAXBSource source = new JAXBSource(jc, myUserData);

StreamResult result = new StreamResult(System.out);

TransformerFactory tf = TransformerFactory.newInstance();
StreamSource xslt = new StreamSource("addMyAttribute.xslt");
Transformer t = tf.newTransformer(xslt);
t.transform(source, result);

If you are implementing your RESTful service using JAX-RS you could plug-in this logic via a MessageBodyWriter:

bdoughan
  • 147,609
  • 23
  • 300
  • 400