5

我想问一个关于jax-ws 中的@UsesJAXBContext注释的问题。我试图让它在客户端工作,但我可能遗漏了一些东西。这是我的情况:

我有网络服务与操作:

@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(...)操作,我需要将对象(我尝试发送)绑定到 JAXBContext 中。我可以通过 在生成的队列存根顶部的@XmlSeeAlso来做到这一点[我试过这个并且它有效]。尽管如此,我需要更通用的解决方案来帮助我在运行时绑定对象。我认为我可以创建@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有效。但对我来说,让它在客户端工作很重要。

4

1 回答 1

6

好的,我可以解决我面临的问题。我仍然无法将@UsesJAXBContext与使用网络服务的客户端一起使用。但我发现此注释与具有后修复功能的 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()));

它有效!

于 2012-06-22T12:11:56.803 回答