Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CXF JAXRS | Complex response types are not present in the generated wadl

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) {
like image 248
crankparty Avatar asked Nov 14 '22 09:11

crankparty


1 Answers

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:

<response><representation mediaType="application/json" element="item"/></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).

like image 90
aliasmrchips Avatar answered Nov 16 '22 03:11

aliasmrchips