我想问一个关于 @UsesJAXBContext的问题 jax-ws 中的注解。我试图让它在客户端工作,但我可能遗漏了一些东西。这是我的案例:
我有网络服务操作:
@WebMethod(operationName = "putToQueue")
public boolean put(@WebParam(name = "queueName") String queueName, @WebParam(name = "element") Object element) {
return queues.get(queueName).offer(element);
}
在客户端,我生成了 QueueService 和 Queue(端口)...以及其他内容...[响应请求。在这种情况下无关紧要。] 我想让用户定义他/她可以放入队列的对象。但是,要调用 put(...) 操作,我需要将(我尝试发送的)对象绑定(bind)到 JAXBContext 中。我可以这样做 @XmlSeeAlso 在生成的队列 stub 的顶部 [我试过这个并且它有效]。尽管如此,我需要更通用的解决方案来帮助我在运行时绑定(bind)对象。 我想我可以创建 @QueueMessage 注释和 ClientJAXBContextFactory 并在创建它时将标记的类添加到上下文中。
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);
}
}
接下来我尝试在生成的队列之上使用@UsesJAXBContext。
@WebService(name = "Queue")
@UsesJAXBContext(ClientJAXBContextFactory.class)
public interface Queue {
...
}
但是 createJAXBContext(...) 没有被调用,jax-ws 只是简单地创建他的 JAXBContextImpl。
我已阅读:
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
还有一些关于 stackOverFlow 的问题。我将不胜感激。 是否可以实现我问题中提出的想法?
此外,我可能会在服务器端添加它...... @UsesJAXBContext 有效。但让它在客户端运行对我来说很重要。
最佳答案
好的,我可以解决我面临的问题。我仍然无法将 @UsesJAXBContext 与使用 web 服务的客户端一起使用。但是我发现这个注释与具有后修复功能的 bean 相关联。所以有一个类 UsesJAXBContextFeature
https://jax-ws.java.net/nonav/2.2.7/javadocs/com/sun/xml/ws/developer/UsesJAXBContextFeature.html
它可以作为端口或服务(自 jax-ws 2.2 以来的服务)的参数传递。我在版本方面遇到了一些麻烦,所以我决定生成类并使用 jax-ws 2.1。现在我只是简单地像这样创建端口:
new QueueService().getQueuePort(new UsesJAXBContextFeature(new ClientJAXBContextFactory()));
而且有效!
关于java - 如何设置自定义 JAXBContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121719/