WADL的问题/使用Jersey的合同优先方法生成的XSD [英] Troubles with WADL / generated XSD using Jersey with a contract-first approach

查看:148
本文介绍了WADL的问题/使用Jersey的合同优先方法生成的XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

要定义以这三种格式交换的对象,我遵循了合同优先"的方法:

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

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

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

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.

为此,感谢 Wiki页面:

For that purpose, thanks to this wiki page:

  • 我已经公开了下面的两个文件
    • /schema/schema.xsd
    • /schema/schema.proto
    • I have exposed the two files under
      • /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>
      

    • 我添加了一个自定义的WADL生成器:

    • I have added a customized WADL generator:

       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中:

      This way the below appears in the WADL, when I hit /rest/application.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的工具失败很可能是因为


      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

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

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

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

      1. 是否有一种方法可以禁用此自动生成的XSD的生成和传播? (因为我遵循这种合同优先"的方法,所以我不需要它)

      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)

      如果没有,是否有办法在按下/rest/application.wadl/xsd0.xsd时用我手动编写的XSD覆盖"其内容? (我在Google周围搜索并找到了有关WadlResource的信息,以生成自定义的WADL,但未发现有关XSD生成本身的信息)

      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)


      提前感谢您的帮助!


      Thanks in advance for your help!

      M.

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

      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

      2)根据帕维尔(Pavel)的指示,我举了票(JERSEY-1230). 我目前正在跟进,要么自己提交修复程序,要么从Jersey团队获取修复程序.

      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.

      推荐答案

      1.14-快照应该允许您执行此操作:

      1.14-SNAPSHOT should allow you to do this:

      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中.

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

      这篇关于WADL的问题/使用Jersey的合同优先方法生成的XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆