【问题标题】:Troubles with WADL / generated XSD using Jersey with a contract-first approachWADL 的问题/使用 Jersey 以合同优先的方法生成的 XSD
【发布时间】:2012-06-07 21:26:21
【问题描述】:

几天来,我一直在使用 Jersey 开发 REST Web 服务,并设法使所有 CRUD 操作正常工作,并使用多种交换格式:XML、JSON、Google Protobuf。

但是我遇到了一些与自动生成的 WADL 和 XSD 相关的问题。


上下文

为了定义以这三种格式交换的对象,我遵循了“契约优先”的方法

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

但是,由于我希望我的用户也能够生成他们的类,我想共享这些架构文件(.xsd 和 .proto)并让它们很好地集成使用自动生成的 WADL

为此,感谢this wiki 页面:

  • 我已经暴露了下面的两个文件
    • /schema/schema.xsd
    • /schema/schema.proto
  • 我添加了一个应用程序语法文件:

    <?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 中的完全不同。 除此之外,在这个 WADL 上调用像 wadl2java 这样的工具会惨遭失败,大概是因为

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

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


问题

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

  2. 如果没有,当/rest/application.wadl/xsd0.xsd 被击中时,有没有办法用我手动编写的 XSD “覆盖”其内容? (我用谷歌搜索并找到了关于 WadlResource 的信息,用于生成自定义的 WADL,但没有找到关于 XSD 生成本身的信息)


提前感谢您的帮助!

M.


编辑

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

2) 根据 Pavel 的指示,我提出了一张罚单 (JERSEY-1230)。 我目前正在跟进,要么自己提交修复,要么从泽西队获得修复。

【问题讨论】:

  • 问题现已修复,请参阅java.net/jira/browse/JERSEY-1230
  • 您应该发布以上内容作为您自己问题的答案,然后接受它(从未回答的 Q 列表中清除它)。

标签: java xsd jersey wadl contract-first


【解决方案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 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多