繁体   English   中英

如何设置自定义JAXBContext

[英]How to set custom JAXBContext

annotation in jax-ws. 我想问一个有关jax-ws中注释的问题。 我尝试使其在客户端运行,但是我可能丢失了一些东西。 这是我的情况:

我有操作的Web服务:

@WebMethod(operationName = "putToQueue")
public boolean put(@WebParam(name = "queueName") String queueName, @WebParam(name = "element") Object element) {
    return queues.get(queueName).offer(element);
}

在客户端,我生成了QueueService和Queue(端口)...以及其他内容... [响应请求。 在这种情况下无关紧要。]我想让用户定义他/她可以放入队列的对象。 I need bind object (that I try to send) into JAXBContext. 但是,要调用操作,我需要将我尝试发送的对象绑定到JAXBContext中。 in the top of the generated Queue stub [i tried this one and it works]. 我可以通过在生成的队列存根顶部执行此操作(我尝试过此操作,它可以工作)。 但是,我需要更通用的解决方案来帮助我在运行时绑定对象。 annotation and 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);
    }
}

on top of generated Queue. 接下来,我尝试在生成的队列上使用

@WebService(name = "Queue")
@UsesJAXBContext(ClientJAXBContextFactory.class)
public interface Queue {
...
}

is not invoked and jax-ws just simply create his JAXBContextImpl. 但是不会调用而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的一些问题。 我将不胜感激。 是否可以实施我的问题中提出的想法?

works. 我也可以在服务器端添加它…… 可以工作。 但是对我来说,使其在客户端运行很重要。

好吧,我可以解决我面临的问题。 with client consuming the webservice. 仍然我无法将与使用Web服务的客户端一起使用。 但是我发现该批注与具有后缀功能的bean绑定在一起。 所以有一个类

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

它可以作为port或service的参数传递(自jax-ws 2.2开始的service)。 我在版本方面遇到了一些麻烦,因此我决定生成类并使用jax-ws 2.1。 现在,我只是简单地创建如下端口:

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

而且有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM