2016-03-25 22 views
4

现状怎样包括泽西WADL类型,同时也返回响应

我有一个返回User对象泽西2.18 API端点。我的利益相关者需要API来生成WADL文件,该文件不仅反映API路径,还反映返回的对象类型。

这是由新泽西覆盖开箱即截至2015年,通过以下端点定义:

@GET 
@Path("/") 
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) 
public User getExampleUser() { 
    User exampleUser = new User(); 
    return exampleUser; 
} 

球衣生成的结果WADL文件中正确包含端点,以及返回类型:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<application xmlns="http://wadl.dev.java.net/2009/02"> 
    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.18 2015-06-05 02:28:21"/> 
    <doc xmlns:jersey="http://jersey.java.net/" jersey:hint="This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter detail. Link: http://localhost:8080/example/api/v3/application.wadl?detail=true"/> 
    <grammars> 
     <include href="application.wadl/xsd0.xsd"> 
      <doc title="Generated" xml:lang="en"/> 
     </include> 
    </grammars> 
    <resources base="http://localhost:8080/example/api/v3/"> 
     <resource path="/"> 
      <method id="getExampleUser" name="GET"> 
       <request> 
       </request> 
       <response> 
        <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="user" mediaType="application/json"/> 
        <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="user" mediaType="application/xml"/> 
       </response> 
      </method> 
     </resource>  
    </resources> 
</application> 

但大多数的州界似乎有端点返回一个更通用Response对象,它允许所有直至并包括E-TAG caching,HTTP状态代码操作,0各种各样的好东西,等等。

例如:

@GET 
@Path("/") 
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) 
public Response getExampleUser() { 
    User exampleUser = new User(); 
    return Response.ok(exampleUser).build(); 
} 

的WADL产生看起来是一样的,但现在的响应部分显示没有返回类型和模式的证据。

<response> 
    <representation mediaType="application/json"/> 
    <representation mediaType="application/xml"/> 
</response> 

我的问题

是否有可能从丰富的自动生成WADL文件中获益,同时还能够让我的终点回到更加灵活Response对象?

或者,我如何处理重定向,缓存和其他基本API功能,同时仍然从我的端点定义中返回特定的对象类型?

+0

哟slifty - 你找到了自己的答案吗?我一直在寻找相同的东西。谢谢! –

回答

1

我有完全相同的问题,能够解决这样的:

  • 我所有的实体必须与@XmlRootElement被注解和顺序发送到假休息终点为他们露面在application.wadl/xsd0.xsd
  • 我在我的项目中包含了Swagger,并用它来生成包含响应代码和响应对象的文档。
  • 然后我运行现有的wadl生成工具,application.wadl
  • 最后一步是编写一个方法,将Swagger文件中的响应代码和对象插入到wadl文件中。

最后三个步骤放在一个休息服务。结果是通过调用其余服务自动生成wadl,并且可以像以前一样继续使用Response