使用带有契约优先方法的Jersey使用WADL /生成XSD的麻烦

时间:2021-12-05 19:34:37

I have been working on a REST web service using Jersey for a few days now, and managed to have all CRUD operations working, with several exchange formats: XML, JSON, Google Protobuf.

我已经使用Jersey工作了几天的REST Web服务,并设法让所有CRUD操作都工作,有几种交换格式:XML,JSON,Google Protobuf。

However I am facing some issues related to automatically generated WADL and XSD.

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


Context

To define the objects exchanged in these three formats, I have followed a "contract-first" approach:

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

  • from a XSD I wrote, I generated my model classes using JAXB;
  • 从我写的XSD中,我使用JAXB生成了我的模型类;
  • from an equivalent proto file I wrote, I generated the Google Protobuf classes (and internally have a way to convert these to the JAXB-generated objects, in order to have one unique model).
  • 从我写的等效原型文件中,我生成了Google Protobuf类(并且内部有一种方法可以将这些转换为JAXB生成的对象,以便拥有一个独特的模型)。

However, as I would like my users to be able to generate their classes too, I would like to share these schema files (.xsd and .proto) and have them well integrated with the automatically generated WADL.

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

For that purpose, thanks to this wiki page:

为此,感谢这个维基页面:

  • I have exposed the two files under
    • /schema/schema.xsd
    • /schema/schema.xsd
    • /schema/schema.proto
    • /schema/schema.proto
  • 我在/schema/schema.xsd /schema/schema.proto下公开了这两个文件
  • I have added an application-grammar file:

    我添加了一个应用程序语法文件:

    <?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>
    
  • I have added a customized WADL generator:

    我添加了一个自定义的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();
        }
     }
    

This way the below appears in the WADL, when I hit /rest/application.wadl:

这样,当我点击/rest/application.wadl时,下面会出现在WADL中:

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

Problem

/rest/application.wadl/xsd0.xsd is automatically generated from my classes, but is quite different from what I initially had in schema.xsd. In addition to that, calling a tool like wadl2java on this WADL fails miserably, presumably because

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

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

are now conflicting (two definitions for the same objects).

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


Questions

  1. Is there a way to disable the generation and diffusion of this automatically generated XSD? (As I don't need it since I'm following this "contract-first" approach)

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

  2. If not, is there a way to "override" its content with my manually written XSD when /rest/application.wadl/xsd0.xsd is hit? (I have googled around and found about WadlResource, to generate customized WADL, but found nothing about the XSD generation itself)

    如果没有,当/rest/application.wadl/xsd0.xsd被击中时,有没有办法用手动编写的XSD“覆盖”其内容? (我已经google了一下,发现了WadlResource,生成了定制的WADL,但没有发现XSD一代本身)


Thanks in advance for your help!

在此先感谢您的帮助!

M.

M.


Edit

1) I raised the issue to the Jersey team and got a reply: http://java.net/projects/jersey/lists/users/archive/2012-06/message/8

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

2) I raised a ticket (JERSEY-1230), according to Pavel's instructions. I am currently following up to either submit a fix myself or get a fix from the Jersey team.

2)根据帕维尔的指示,我提出了一张票(JERSEY-1230)。我目前正在跟进自己提交修复或从泽西团队获得修复。

1 个解决方案

#1


1  

1.14-SNAPSHOT should allow you to do this:

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();
    }

}

when overrideGrammars is set to true, Jersey generated grammars won't be included in returned WADL.

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

#1


1  

1.14-SNAPSHOT should allow you to do this:

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();
    }

}

when overrideGrammars is set to true, Jersey generated grammars won't be included in returned WADL.

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