문제

I'm looking to have some conditional marshalling completed with jaxb. Something like this:

Class A{

//Only marshall when condition X applies
public String fieldOne;

//Only marshall when condition Y applies
public String fieldTwo;

//Always marshall
public String fieldThree;

}

Essentially I have 2 different Web Service methods which use the same model, but I need the information sent to be different on each of these web service methods.

My best option so far would be to create a custom XMLJavaTypeAdapter which verifies some conditional logic. The adapter would return null when I don't want the object, when I do need it marshalled it would return itself.

I'm looking to see if anyone has a better alternative. My jaxb context is quite complex and already has a few layer of adapters.

Thanks in advance.

도움이 되었습니까?

해결책

My best option so far would be to create a custom XMLJavaTypeAdapter which verifies some conditional logic. The adapter would return null when I don't want the object, when I do need it marshalled it would return itself.

I've been there and done that, it gets very messy very fast. If you can use MOXy (I see your post is tagged with moxy), you can can use the XmlNamedObjectGraph annotation to create named profiles of elements that are included when you instance is serialized.

Blaise Doughan (team lead for the MOXy project) explains it better than I can.

Blaise's blog post shows how to use annotations, but he also wrote a page on the EclipseLink wiki that shows how to do it programmatically.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top