使用基于契约的方法生成WADL / XSD时出现问题(使用Jersey框架)

55

我已经在使用Jersey构建REST web服务的几天里成功实现了所有CRUD操作,并支持多种交换格式:XML、JSON和Google Protobuf。

然而,我面临与自动生成的WADL和XSD相关的问题。


背景

为了定义在这三种格式中交换的对象,我采用了“契约优先”的方法:

  • 我编写了一个XSD文件,使用JAXB生成了我的模型类;
  • 从我编写的等效proto文件中,我生成了Google Protobuf类(并内部有一种方法将其转换为JAXB生成的对象,以便拥有一个唯一的模型)。

然而,由于我希望用户也能够生成他们的类,我想共享这些模式文件(.xsd和.proto),并将它们与自动生成的WADL很好地集成。

出于这个目的,感谢这个维基页面:

  • 我将这两个文件公开为
    • /schema/schema.xsd
    • /schema/schema.proto
  • 我添加了一个application-grammar文件:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <grammars xmlns="http://wadl.dev.java.net/2009/02" 
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:xi="http://www.w3.org/1999/XML/xinclude">
        <include href="../schema/schema.xsd" />
    </grammars>
    
    我添加了一个定制的WADL生成器:
  •  public class RichWadlGeneratorConfig extends WadlGeneratorConfig {
        @Override
        public List<WadlGeneratorDescription> configure() {
            return generator(WadlGeneratorApplicationDoc.class)
                .prop("applicationDocsStream", "application-doc.xml")
                .generator(WadlGeneratorGrammarsSupport.class)
                .prop("grammarsStream", "application-grammars.xml")
                .descriptions();
        }
     }
    

当我访问/rest/application.wadl时,以下内容将显示在WADL中:

<grammars>
     <include href="../schema/schema.xsd"/>
     <include href="application.wadl/xsd0.xsd">
          <doc title="Generated" xml:lang="en"/>
     </include>
</grammars>

问题

/rest/application.wadl/xsd0.xsd 是从我的类自动生成的,但是与我最初拥有的schema.xsd不同。 此外,调用像 wadl2java 这样的工具在这个 WADL 上失败了,可能是因为

  • /schema/schema.xsd
  • /rest/application.wadl/xsd0.xsd

现在冲突了(相同对象的两个定义)。

问题

  1. 有没有办法禁用此自动生成的 XSD 的生成和传播? (因为我遵循这种“契约优先”的方法,所以我不需要它)

  2. 如果不能,是否有办法在访问/rest/application.wadl/xsd0.xsd 时使用我手动编写的 XSD 覆盖其内容? (我已经搜索过并了解到了关于 WadlResource 的信息,以生成定制的 WADL,但是没有找到有关 XSD 生成本身的任何信息)

Edit

1) 我向 Jersey 团队提出了问题,并得到了回复: http://java.net/projects/jersey/lists/users/archive/2012-06/message/8

2) 我按照 Pavel 的指示提出了一个票(JERSEY-1230)。 我目前正在跟进,以提交自己的修复或从 Jersey 团队获得修复。


2
问题已经解决,详情请见http://java.net/jira/browse/JERSEY-1230。 - Pavel Bucek
你应该将上述内容发布为自己问题的答案,然后接受它(以清除未回答的问题列表)。 - scottb
1个回答

1

1.14-SNAPSHOT应该允许您完成此操作:

public class SampleWadlGeneratorConfig extends WadlGeneratorConfig {

    @Override
    public List<WadlGeneratorDescription> configure() {
        return generator( WadlGeneratorApplicationDoc.class )
                .prop( "applicationDocsStream", "application-doc.xml" )
                .generator( WadlGeneratorGrammarsSupport.class )
                .prop( "grammarsStream", "application-grammars.xml" )
                .prop("overrideGrammars", true)                               // !!!
                .generator( WadlGeneratorResourceDocSupport.class )
                .prop( "resourceDocStream", "resourcedoc.xml" )
                .descriptions();
    }

}

当overrideGrammars设置为true时,Jersey生成的语法不会包含在返回的WADL中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接