CXF JAXRS | CXF生成的 wadl 中不存在复杂的响应类型
我们使用 cxf 2.5.2 和 spring 来公开和使用 Restful 服务。 为了分发服务接口类,我们开始使用 wadl2java 目标(它根据给定的 wadl 文件生成接口类)
生成的 wadl 不包含正确的响应类型,因此我猜测,生成的接口都将“Response”作为返回类型。
前任。如果 Restful get 方法返回 'List' ,则生成的 wadl 仅包含以下段:
和从此 wadl 文件生成的相应接口包含返回类型为“响应”
有人可以建议需要做什么来防止实际响应类型丢失吗? 是否需要任何注释(例如 ElementClass?如何使用它?)或提供程序?
当前代码:
@GET
@Path("/itemsForCategory")
@Produces("application/json")
@Description("getItemsForCategory")
public List<Item> getItemsForCategory(@QueryParam("category")String category) {
We use cxf 2.5.2 along with spring for exposing and consuming restful services.
For distributing the service interface classes, we started using wadl2java goal (which generates interface classes based on the given wadl file)
The generated wadl doesnt contain the proper response type, because of which i guess, the generated interfaces all have 'Response' as the return type.
Ex. if the restful get method returns 'List' , the generated wadl contains the following segment only:
<response><representation mediaType="application/json"/></response>
and the corresponding interface generated from this wadl file contains the return type as 'Response'
Can someone suggest what needs to be done to prevent the actual response type from getting lost?
Are any annotations (like ElementClass ? how to use it ?) or providers required?
Current code:
@GET
@Path("/itemsForCategory")
@Produces("application/json")
@Description("getItemsForCategory")
public List<Item> getItemsForCategory(@QueryParam("category")String category) {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通用的“Response”返回类型似乎与您尝试返回列表的事实无关。也就是说,即使使用“Item”作为返回类型也会导致生成的接口中的方法的返回类型为“Response”。为了解决这个问题,您需要在 WADL 资源响应中添加元素属性:
如果您直接修改 WADL,这将起作用,等效的 JAX-RS 注释可能受支持,也可能不支持。这也不能解决您返回列表的问题。我的建议(我之前使用过)是创建一个封装 List 返回类型的包装列表类型(例如 ItemList)。
无论哪种情况,您都需要从自下而上翻转为自上而下(即首先使用 WADL)实现。这应该不会太糟糕,因为您已经有了实现,并且可以让它实现生成的接口。
为了澄清这一切,我基于标准 JAX-RS“Bookstore”示例制作了一个简单的示例项目。您可以查看 pom (带有 wadl2java 配置)和实际的 < a href="https://github.com/aliasmrchips/Bookstore/blob/master/src/main/resources/wadl/bookstoreImport.xml" github 上的 rel="nofollow">wadl。生成的代码也在那里(例如, BookstoreidResource.java)。
The generic "Response" return type seems to be unrelated to the fact that you are trying to return a list. That is, even using "Item" as the return type would result in a method in the generated interface with a return type of "Response". To remedy this, you need to add the element attribute in the WADL resource response:
This works if you modify the WADL directly, an equivalent JAX-RS annotation may or may not be supported. This also does not address your problem returning a list. My suggestion (which I have previously used) is to create a wrapper list type (e.g. ItemList) that encapsulates the List return type.
In either case, you will need to flip from a bottom up to a top down (i.e., WADL first) implementation. This should not be too bad, since you already have the implementation and you can just make it implement the generated interface.
To clarify all this, I made a simple example project based on the standard JAX-RS "Bookstore" example. You can view the pom (with the wadl2java configuration) and the actual wadl on github. The generated code is there as well (e.g., BookstoreidResource.java).
我在处理列表、映射等时遇到了类似的问题。因为在生成 WSDL 时集合在运行时不知道它们的类型,所以您放入集合中的类型将被忽略。我发现,例外情况是另一个 Web 服务公开方法使用该特定类型时。作为解决方法,我创建了一个虚拟方法,该方法使用列表和地图所需的每种类型。
例如,我有一个名为 User 的类,它扩展了一个名为 BaseObject 的抽象类,但 Web 服务并未直接使用该抽象类。然而,在搜索用户时有时会通过列表进行传递。以下代码是我的解决方法。
我承认这是一个有点令人讨厌的解决方案,并且我不认为这是一个正确的解决方案,但也许它会让您继续前进,直到有人可以提供更好的解决方案。
希望这有帮助。
I had similar issues when dealing with lists, maps etc. Because collections don't know their type at runtime when generating a WSDL the types that you put into the collection are ignored. The exception to this, I found, was when another web service exposed method used that particular type. As a work around I created a dummy method that used every type I needed for lists and maps.
So for example, I had a class called User that extended an abstract class called BaseObject that was not used directly by the webservice. However it was sometimes passed through lists when searching for users. The following code was my workaround.
I admit this is a bit of a nasty work around and I don't consider it a proper fix, but maybe it will keep you going until someone can provide a better solution.
Hope this helps.