且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

定制 SOAP 响应的 JAX-WS 前缀

更新时间:2023-09-11 22:18:46

如果您从旧服务的 WSDL 开始,并使用 wsimport 生成所有各种带有 JAXB 注释的请求和响应包装类,那么在生成的包中你应该找到一个 package-info.java 例如

If you started from the WSDL of the old service and generated all the various JAXB annotated request and response wrapper classes using wsimport, then in the generated package you should find a package-info.java such as

@javax.xml.bind.annotation.XmlSchema(namespace = "http://test/")
package com.example.test;

JAXB 为您提供了一种机制,可以在 @XmlSchema 注释上建议前缀映射,因此您可以尝试修改 package-info.java 以读取

JAXB provides a mechanism for you to suggest prefix mappings on the @XmlSchema annotation, so you could try modifying package-info.java to read

@javax.xml.bind.annotation.XmlSchema(namespace = "http://test/",
   xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "customns", 
         namespaceURI="http://test/")
   }
)
package com.example.test;

看看这对生成的消息是否有任何影响.这还具有纯 JAXB 规范的优势(即不依赖于 RI 特定的自定义上下文工厂).

and see if that makes any difference to the generated messages. This also has the advantage of being pure JAXB spec (i.e. not dependent on the RI-specific custom context factory).

如果您需要重新运行 wsimport,您可以通过将 -npa 选项传递给xjc (这告诉它不要生成 package-info.java 而是将所有必要的 namespace 设置放在类级注释上反而).具体如何执行此操作取决于您运行 wsimport 的方式.

If you need to re-run wsimport you can prevent it from overwriting your modified package-info by passing the -npa option to xjc (this tells it not to generate a package-info.java but instead to put all the necessary namespace settings on the class-level annotations instead). Exactly how you do this depends how you're running wsimport.

命令行:

wsimport -B-npa ....

蚂蚁:

<wsimport wsdl="..." destdir="..." .... >
  <xjcarg value="-npa" />
</wsimport>

马文:

<plugin>
  <groupId>org.jvnet.jax-ws-commons</groupId>
  <artifactId>jaxws-maven-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <goals>
        <goal>wsimport</goal>
      </goals>
      <configuration>
        <xjcArgs>
          <xjcArg>-npa</xjcArg>
        </xjcArgs>
      </configuration>
    </execution>
  </executions>
</plugin>