In the investigations I've been doing for my project, I've come to the following conclusions...
Jaxb2 is faaar better than Jaxb1. Avoid Jaxb1 at all costs.
Jaxb2, in regards to Web Service projects, is best used from a Schema First approach. Design your XSD, use the xjc2 ant task to generate the Annotated java code.
Also, when it comes to the OXM stuff in Spring-WS, I subclassed the AbstractMarshallingPayloadEndpoint to make our endpoints a little simpler. The idea being that we would have many endpoints, each with their own marshaller.
As this is a Java5 project (using jaxb2 annotations and such) the "AbstractJaxb2MarshallingPayloadEndpoint uses generics as well.
Code:
public abstract class AbstractJaxb2MarshallingEndpoint<RQ, RS> extends AbstractMarshallingPayloadEndpoint {
protected AbstractJaxb2MarshallingEndpoint(Class... classesToBeBound) {
Jaxb2Marshaller marshaller = buildMarshaller(classesToBeBound);
setMarshaller(marshaller);
setUnmarshaller(marshaller);
}
private Jaxb2Marshaller buildMarshaller(Class... classesToBeBound) {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(classesToBeBound);
initializeMarshaller(marshaller);
try {
marshaller.afterPropertiesSet();
} catch (Exception e) {
throw new RuntimeException("Failed to initialize Jaxb2Marshaller", e);
}
return marshaller;
}
protected void initializeMarshaller(Jaxb2Marshaller marshaller) {
}
@SuppressWarnings("unchecked")
protected final Object invokeInternal(Object object) throws Exception {
return invokeEndpoint((RQ) object);
}
protected abstract RS invokeEndpoint(RQ request) throws Exception;
}
Our final endpoint looks like this then...
Code:
public class CurrencyExchangeEndpoint extends AbstractJaxb2MarshallingEndpoint<ExchangeRequest, ExchangeResponse> {
private final CurrencyExchangeProcessor currencyExchangeProcessor;
public CurrencyExchangeEndpoint(CurrencyExchangeProcessor currencyExchangeProcessor) {
super(ExchangeRequest.class, ExchangeResponse.class);
this.currencyExchangeProcessor = currencyExchangeProcessor;
}
protected ExchangeResponse invokeEndpoint(ExchangeRequest request) throws Exception {
return currencyExchangeProcessor.execute(request);
}
}