java - 如何在 Jersey WADL 中包含类型同时返回 Response

标签 java rest jax-rs jersey-2.0

情况

我有一个返回 User 对象的 Jersey 2.18 API 端点。我的涉众需要 API 来生成一个 WADL 文件,该文件不仅反射(reflect) API 路径,而且反射(reflect)返回的对象类型。

自 2015 年起,Jersey 已通过使用以下端点定义开箱即用:

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

Jersey 生成的结果 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>

但大多数 Jersey 社区似乎都有端点返回更通用的 Response对象,它允许各种美好的事物,包括 E-TAG caching , HTTP 状态码操作, error messaging ,等等。

例如:

@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 功能,同时仍然从我的端点定义返回特定对象类型?

最佳答案

我遇到了完全相同的问题,并且能够像这样解决它:

  • 我的所有实体都必须用 @XmlRootElement 进行注释,并发送到虚拟 rest 端点,以便它们显示在 application.wadl/xsd0.xsd 中.
  • 我包括了Swagger在我的项目中,并将其用于生成包含响应代码和响应对象的文档。
  • 然后我运行了现有的 wadl 生成工具 application.wadl
  • 最后一步是编写一个方法,将 Swagger 文件中的响应代码和对象插入到 wadl 文件中。

最后三个步骤放在休息服务中。结果是调用rest服务自动生成了wadl,可以像之前一样继续使用Response

关于java - 如何在 Jersey WADL 中包含类型同时返回 Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36212097/

相关文章:

java.lang.NoSuchMethodError : javax. ws.rs.core.Application.getProperties()Ljava/util/Map;将 jersey ws 部署到 Weblogic 时

java - 使用 Feign 客户端发布未知类的 json

c# - 面向对象游戏编程中的物理

java - 从 Java 中的 Lambda 函数调用外部 Rest API

java - 带拦截器的 JAX-RS/Wildfly/Java SDK13 方法调用失败

java - 如何使用 reSTLet 从 jax-rs 获取客户端的 ip

java - Android 调试器无法连接

java - 连接到 Microsoft SQL Server 2005 中的数据库引擎

scala - Akka Http 客户端 :Custom headers

java - 对 REST API 类感到困惑