How to set custom JAXBContext

annotations, java, java-metro-framework, jax-ws, jaxb

Solution

Ok I could manage problem i was facing. Still i couldn't use @UsesJAXBContext with client consuming the webservice. But i found that this annotations are tied to beans with post-fix Feature. So there is a class UsesJAXBContextFeature

https://jax-ws.java.net/nonav/2.2.7/javadocs/com/sun/xml/ws/developer/UsesJAXBContextFeature.html

and it could be passed as argument of port or service(service since jax-ws 2.2). I have got a little trouble with versions so i decided to generate class and use jax-ws 2.1. Now i just simply create port like this:

new QueueService().getQueuePort(new UsesJAXBContextFeature(new ClientJAXBContextFactory()));

And it works!

Problem

i would like to ask a question about @UsesJAXBContext annotation in jax-ws. I try to make it work on client side but I'm probably missing something. Here is my case: I've got webservice with operation: ``` @WebMethod(operationName = "putToQueue") public boolean put(@WebParam(name = "queueName") String queueName, @WebParam(name = "element") Object element) { return queues.get(queueName).offer(element); } ``` On the client side i generated QueueService and Queue (port)... and other stuff... [respones requests. In this case irrelevant.] I would like to let user define object that he/she could put to queue. However to invoke operation put(...) I need bind object (that I try to send) into JAXBContext. I could do that by @XmlSeeAlso in the top of the generated Queue stub [i tried this one and it works]. Nonetheless I need more generic solution that help me bind object at runtime. I thought that I could create @QueueMessage annotation and ClientJAXBContextFactory and add marked class to the context when creating it. ``` public class ClientJAXBContextFactory implements JAXBContextFactory { @Override public JAXBRIContext createJAXBContext(SEIModel seim, List<Class> classes, List<TypeReference> references) throws JAXBException { Reflections reflections = new Reflections(""); Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(QueueMessage.class); classes.addAll(annotated); return JAXBContextFactory.DEFAULT.createJAXBContext(seim, classes, references); } } ``` Next i tried use @UsesJAXBContext on top of generated Queue. ``` @WebService(name = "Queue") @UsesJAXBContext(ClientJAXBContextFactory.class) public interface Queue { ... } ``` But createJAXBContext(...) is not invoked and jax-ws just simply create his JAXBContextImpl. I have read: http://weblogs.java.net/blog/jitu/archive/2008/08/control_of_jaxb.html http://www.techques.com/question/1-5627173/Specify-JAXB-Packages-in-SLSB-and-JAX-WS and some question on stackOverFlow. I would be grateful for advises. Is it possible to implement idea presented in my question? Also i might add that on the server side ... @UsesJAXBContext works. But its important for me to make it work on the client side.

Original source