草庐IT

java - JAX-WS:独立于 WSDL 的编译模式

coder 2023-05-18 原文

如果有一个相当大的模式用于多个 Web 服务,因此我想将 XSD 编译与 WSDL 编译分开。在一个简化的示例中,一步编译即可:

$ wsimport -verbose service.wsdl
parsing WSDL...

Generating code...

org/example/wsdl/mysvc/MySvcPortType.java
org/example/wsdl/mysvc/MySvcService.java
org/example/ns1/Element1.java
org/example/ns1/ObjectFactory.java
org/example/ns1/package-info.java
...

编译 xsd 并使用生成的剧集文件不起作用:

$ wsimport -b schema3.episode service.wsdl  
parsing WSDL...

[ERROR] Schema descriptor {http://www.example.org/ns1}element1 in
message part "part1" is not defined and could not be bound to Java.
Perhaps the schema descriptor {http://www.example.org/ns1}element1 is
not defined in the schema imported/included in the WSDL. You can
either add such imports/includes or run wsimport and provide the
schema location using -b switch.   line 9 of
file:...jaxepisode_element/service.wsdl

那么如何在 wsimport 中使用预编译的架构呢?

附录: 在包含在另一个模式中时使用剧集并避免新编译(d3.jar 包含从 schema3 + schema3.episode 生成的类作为 META-INF/sun-jaxb.episode):

$ xjc schema4.xsd d3.jar -extension
parsing a schema...

compiling a schema...

org/example/ns2/Element2.java
org/example/ns2/ObjectFactory.java
org/example/ns2/package-info.java

没有预编译包:

$ xjc schema4.xsd  -extension
parsing a schema...
compiling a schema...
org/example/ns1/Element1.java
org/example/ns1/ObjectFactory.java
org/example/ns1/package-info.java
org/example/ns2/Element2.java
org/example/ns2/ObjectFactory.java
org/example/ns2/package-info.java

WSDL:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.org/wsdl/MySvc" xmlns:ns="http://www.example.org/ns1" targetNamespace="http://example.org/wsdl/MySvc" name="MySvc">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://www.example.org/ns1" schemaLocation="schema3.xsd"/>
    </xsd:schema>
  </types>
  <message name="myOpRequest">
    <part name="part1" element="ns:element1"/>
  </message>
  <message name="myOpReply">
    <part name="part1" element="ns:element1"/>
  </message>
  <portType name="MySvcPortType">
    <operation name="myOp">
      <input name="input1" message="tns:myOpRequest"/>
      <output name="output1" message="tns:myOpReply"/>
    </operation>
  </portType>
  <binding name="MySvcBinding" type="tns:MySvcPortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="myOp">
      <soap:operation/>
      <input name="input1">
        <soap:body use="literal"/>
      </input>
      <output name="output1">
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="MySvcService">
    <port name="MySvcPort" binding="tns:MySvcBinding">
      <soap:address location="http://localhost:8080/"/>
    </port>
  </service>
</definitions>

schema3.xsd:

<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/ns1" 
    xmlns:tns="http://www.example.org/ns1" 
    elementFormDefault="qualified">
  <element name="element1" >
    <complexType >
        <sequence>
                <element name="name" type="string" />
        </sequence>
     </complexType>
   </element>
</schema>

schema4.xsd:

<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/ns2" 
    xmlns:tns="http://www.example.org/ns2" 
    xmlns:ns1="http://www.example.org/ns1" 
    elementFormDefault="qualified">

    <import namespace="http://www.example.org/ns1" schemaLocation="schema3.xsd"/> 
    <element name="element2" >
      <complexType >
        <sequence>
          <element ref="ns1:element1" />
        </sequence>
       </complexType>
     </element>
</schema>

生成的剧集文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
  <bindings scd="x-schema::tns" xmlns:tns="http://www.example.org/ns1">
    <schemaBindings map="false">
      <package name="org.example.ns1"/>
    </schemaBindings>
    <bindings scd="tns:element1">
      <class ref="org.example.ns1.Element1"/>
    </bindings>
  </bindings>
</bindings>

最佳答案

您可以使用 maven,分别使用 Apache CXF 生成。

我有一个可以帮助你的例子。

属性:

 <properties>
    <apache.cxf.version>3.0.4</apache.cxf.version>
    <cxf-codegen-plugin.version>3.0.4</cxf-codegen-plugin.version>
    <cxf-xjc-plugin.version>3.0.3</cxf-xjc-plugin.version>
 </properties>

依赖:

 <dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${apache.cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${apache.cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-databinding-jaxb</artifactId>
        <version>${apache.cxf.version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
            </exclusion>
            <exclusion>
                <groupId>xalan</groupId>
                <artifactId>xalan</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
 </dependencies>

运行 --> mvn clean install -Pgenerates-nfe-services

   <profile>
        <id>generates-nfe-services</id>
        <activation>
            <property>
                <name>generates-nfe-services</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-codegen-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <!--sourceRoot>${project.build.directory}/generated-sources</sourceRoot-->
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                                <defaultOptions>
                                    <extraargs>
                                        <extraarg>-validate</extraarg>
                                        <extraarg>-client</extraarg>
                                        <extraarg>-verbose</extraarg>
                                        <extraarg>-xjc-npa</extraarg>
                                        <extraarg>-xjc-verbose</extraarg>
                                        <extraarg>-xjc-extension</extraarg>
                                        <extraarg>-exsh</extraarg>
                                        <extraarg>true</extraarg>
                                        <extraarg>-keep</extraarg>
                                    </extraargs>
                                </defaultOptions>
                                <wsdlOptions>
                                    <wsdlOption>
                                        <wsdl>
                                            ${basedir}/src/main/wsdl/br/gov/rs/sefaz/nfe/homologacao/NfeConsultaCadastro.wsdl
                                        </wsdl>
                                        <extraargs>
                                            <extraarg>-p</extraarg>
                                            <extraarg>br.gov.rs.sefaz.nfe.consulta.cadastro</extraarg>
                                        </extraargs>
                                    </wsdlOption>
                                </wsdlOptions>
                            </configuration>
                            <goals>
                                <goal>wsdl2java</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

运行 --> mvn clean install -Pgenerates-layouts-nfe

    <profile>
        <id>generates-layouts-nfe</id>
        <activation>
            <property>
                <name>generates-layouts-nfe</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-xjc-plugin</artifactId>
                    <version>${cxf-xjc-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>xsdtojava</goal>
                            </goals>
                            <configuration>
                                <!--sourceRoot>${basedir}/target/generated-sources</sourceRoot-->
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                                <xsdOptions>
                                    <xsdOption>
                                        <xsd>${basedir}/src/main/schemas/nfe/PL_008f/leiauteConsSitNFe_v3.10.xsd
                                        </xsd>
                                        <packagename>br.inf.portalfiscal.nfe.v310.leiaute.consulta.situacao.nfe
                                        </packagename>
                                        <extension>true</extension>
                                        <extensionArgs>
                                            <!--extensionArg>-npa</extensionArg-->
                                            <extensionArg>-extension</extensionArg>
                                        </extensionArgs>
                                    </xsdOption>
                                </xsdOptions>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

您可以创建一个仅生成 wsdl 的 maven 项目,而另一个仅生成 xsd。

注意:

如果他的项目不需要使用 WSDL,请切换到 json-rest-api。

使用 REST 并快乐。\o/

我希望这会有所帮助。

关于java - JAX-WS:独立于 WSDL 的编译模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28901524/

有关java - JAX-WS:独立于 WSDL 的编译模式的更多相关文章

  1. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  4. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  5. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  6. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

  7. ruby - Sinatra set cache_control to static files in public folder编译错误 - 2

    我不知道为什么,但是当我设置这个设置时它无法编译设置:static_cache_control,[:public,:max_age=>300]这是我得到的syntaxerror,unexpectedtASSOC,expecting']'(SyntaxError)set:static_cache_control,[:public,:max_age=>300]^我只想将“过期”header设置为css、javaascript和图像文件。谢谢。 最佳答案 我猜您使用的是Ruby1.8.7。Sinatra文档中显示的语法似乎是在Ruby1.

  8. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  9. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  10. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

    这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

随机推荐