I have the following model
class MyClass {
id
someRandomString
}
I would like to return this POJO to the client as…
<Root>
<random>if + randomstring</random>
</Root>
Basically I’m dealing with a very message oriented service so I need to wrap and transform the model to the outbound XML format… Same goes on the input side
I have this XML…
<Root>
<Username>
<Password>
<Action> <-- Some action or service to perform
<SomeModel1>
<SomeModel2>
</Root>
So the root and username and password are constant elements while the SomeModel can change based on the “service”
Bassically i’m looking for a design-pattern where the XML doesn’t match the model and there needs to be a transformation to and from…
Or what if I did…
class MyClass {
id
someRandomString
random
@XMLTransient
getId()
@XMLTransient
getSomeRandomString()
getRandom() {
return id + someRandomString
}
}
So JAXB will only map getRandom…
Another idea I have is create a bunch of classes that will represent the final output and just set the values to thoses…
So
class MyResponse {
randomString
status
someOtherFieldRequired in response
}
and then I can do…
myResponse.setRandomString(myClass.getId() + myClass.getSomeRandomString());
You could use XmlAdapter in conjunction with XmlJavaTypeAdapter, but I think it might require you to encapsulate the id and someRandomString properties into a separate class. Spreading out an element value over two separate bean properties directly is not something I’d know how to do, if it is at all possible.