Axis2 Web 服务,自下而上方法,复杂对象
我正在使用 axis2 将现有类的方法公开为 Web 服务(自下而上的方法)。该方法接受一个复杂对象(非原始类型)作为参数,并返回一个复杂对象。
据我了解,当我将该方法公开为 Web 服务时,axis2 将尝试在 wsdl 文件中为我生成架构,并且我可以通过访问 Web 服务 url 并将 ?wsdl 附加到其末尾来获取 wsdl 文件。
但经过仔细检查,参数中复杂类型的一些属性在生成的 wsdl 的模式部分中表示为 xs:anyType。转换为 xs:anyType 的属性是一个 List。这样做的坏处是,当我为客户端代码生成存根代码时,设置该特定属性的方法签名将接受一个对象作为参数,即 setAttribute(Object obj)。
所以我的解决方案是使用 JAXB 2.0 生成我需要的类的 xml 模式,然后将 xsd 导入到 axis2 生成的 wsdl 文件(从 Web 服务 url + ?wsdl 下载)并使用编辑后的wsdl 而不是自动生成的。这个解决方案似乎对客户端来说效果很好。用于设置存根代码生成的属性的方法签名将采用正确的类型,即 setAttribute(AnotherComplexType abcd)。通过使用 tcpmon,我可以看到从客户端发送到服务器的 xml 似乎是正确的。
但是,这种方法对于服务器端来说效果不佳,因为 axis2 不使用 JAXB 2.0 注释将接收到的 xml 转换回公开方法能够处理的类。
我的问题是,有办法解决我的问题吗?我能想到的可能的方法是要么修改axis2在收到xml后处理xml的方式(如果确实有办法的话我可以手动处理它),或者让axis2与JAXB 2.0注释一起很好地工作?或者也许还有其他想法?
注意:我没有使用 axis2 的 JAX-WS 部分
I am using axis2 to expose a method of existing class as a web service (bottom-up approach). The method takes a complex object (non-primitive type) as a parameter and returns a complex object as well.
I understand that axis2 will try to generate the schema for me in the wsdl file when I expose the method as a web service, and I can get the wsdl file by visiting the web service url and append ?wsdl into the end of it.
But upon closer examination, some of the attributes of the complex type in the parameters are represented as xs:anyType in the schema part of the resulting wsdl. The attributes that are converted into xs:anyType is a List. The bad thing with this is that when I generate the stub code for the client code, the method signature to set that particular attributes will take in an object as a parameter i.e. setAttribute(Object obj).
So my solution to this is to use JAXB 2.0 to generate the xml schema of the classes I need and then, import the xsd into the wsdl file that is generated by axis2 (downloaded from the web service url + ?wsdl) and use the edited wsdl instead of the one automatically generated. This solution seems to be working well for the client side. The method signature to set the attributes generated by the stub code will take in the proper type i.e. setAttribute(AnotherComplexType abcd). And by using tcpmon, I can see that the xml that is sent from the client to the server seems to be correct.
However, this approach does not work well for the server side because axis2 does not use the JAXB 2.0 annotation to convert the xml received back into the classes that the exposed method will be able to process.
My question is, is there anyway to solve my problem? The possible ways I can think of is either to modify the way axis2 process the xml after receiving it (I'm okay with processing it manually if there is indeed a way), or to make axis2 work well with JAXB 2.0 annotation? Or maybe any other idea?
Note: I'm not using the JAX-WS part of axis2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我自己解决了这个问题。我使用 wsdl 生成服务器端的存根代码,修改 messageReceivers 以使用生成的消息接收器,编写一个实现生成的接口的包装类,编写一个转换器以从包装类的参数中生成的类型转换对象方法将暴露给我的内部类型并暴露包装类。
In the end I solved this myself. I generated the stub code for server side using the wsdl, modify the messageReceivers to use the generated message receiver instead, write a wrapper class that implements the generated interface, write a converter to convert the object from generated types in the parameter of the wrapper class methods going to be exposed to my internal types and expose the wrapper class instead.