创建 XSD 并将其链接到 WADL

发布于 2024-11-10 11:49:18 字数 1077 浏览 3 评论 0原文

我正在使用 JAX-RS 创建一些服务,这些服务需要接受任意复杂的对象作为参数,而不仅仅是整数和字符串等基元。 CXF 邮件列表上的讨论 表示在这种情况下仅使用包装器对象作为单个参数。

我关心的是如何记录服务的输入格式?如果创建如下所示的服务:

@POST
@Produces("application/json")
@Consumes("application/json")
@Path("oneParam")
public ComplexObject2 myServiceMethod(ComplexObject1 obj) {
    Foo f = obj.foo
    Bar b = obj.bar
    ...
}

CXF 生成的自动生成的 WADL 将仅生成以下内容:

<resource path="/oneParam">
   <method name="POST">
      <request>
            <representation mediaType="application/json"/>
      </request>
      <response>
             <representation mediaType="application/json"/>
       </response>
   </method>
</resource> 

这不包含有关请求或响应实际包含的内容的信息。 CXF 邮件列表上的 Sergey 表示可以将模式链接到表示,但我应该怎么做呢?如何创建 XSD?

(PS 对幂等资源使用 POST 可能不是 RESTful,但这在这里并不重要,因为我们本质上是使用 Json 进行 RPC。这或多或少是现有基于 SOAP 的 api 的 1:1 克隆。)

I am creating some services using JAX-RS that need to take in arbitrarily complex objects as arguments, not just primitives like integers and strings. A discussion on the CXF mailing list says to just use a wrapper object as a single parameter in this case.

My concern is how to document the input format to the service? If creating a service that looks something like the following:

@POST
@Produces("application/json")
@Consumes("application/json")
@Path("oneParam")
public ComplexObject2 myServiceMethod(ComplexObject1 obj) {
    Foo f = obj.foo
    Bar b = obj.bar
    ...
}

the auto-generated WADL that CXF produces will only produce the following:

<resource path="/oneParam">
   <method name="POST">
      <request>
            <representation mediaType="application/json"/>
      </request>
      <response>
             <representation mediaType="application/json"/>
       </response>
   </method>
</resource> 

This contains no information on what the request or response actually contains. Sergey on the CXF mailing list said it was possible to link a schema to the representation, but how am I supposed to do that? And how do I create the XSD?

(P.S. Using POST for idempotent resources might not be RESTful, but it's not important here as we are in essence doing RPC using Json. This is more or less a 1:1 clone of an existing SOAP based api.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2024-11-17 11:49:18

可以将 XSD 文件链接到 WADL 文件,然后在请求和响应的表示中引用 XML 元素。但是,由于它是 XML 架构,因此不适用于 JSON 表示。

要将 XSD 链接到 WADL 文件,请在文件顶部的主 resources 元素之前创建一个 grammars 元素。

<grammars>
    <include href="myapp.xsd"/>
</grammars>

然后添加对 XML 元素的引用,如下所示(使用示例的修改版本):

<resource path="/oneParam">
   <method name="POST">
      <request>
            <representation mediaType="application/xml" element="myapp:oneParamRequest" />
      </request>
      <response>
             <representation mediaType="application/xml" element="myapp:oneParamResponse" />
       </response>
   </method>
</resource>

前缀 myapp 在 XSD 中定义,也可以在 WADL 文件中使用。

我不知道如何配置 CXF 自动执行此操作。我对 Jersey 的体验与此类似,我们使用生成的 WADL 作为稍后手动编辑的起点。

It is possible to link an XSD file into a WADL file and then to reference an XML element in the representation for requests and responses. However, as it is XML schema it doesn't apply to JSON representations.

To link an XSD into a WADL file, create a grammars element at the top of the file before the main resources element.

<grammars>
    <include href="myapp.xsd"/>
</grammars>

Then add a reference to an XML element as follows (using a modified version of your example):

<resource path="/oneParam">
   <method name="POST">
      <request>
            <representation mediaType="application/xml" element="myapp:oneParamRequest" />
      </request>
      <response>
             <representation mediaType="application/xml" element="myapp:oneParamResponse" />
       </response>
   </method>
</resource>

The prefix myapp is defined in the XSD and can be used in the WADL file as well.

I don't know to to configure CXF to do this automatically. My experience with Jersey is similar and we use the generated WADL as a starting point for hand-editing later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文