java wsimport 客户端 - 自定义多个包名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27779697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wsimport client - customise multiple package names
提问by user93353
I am using wsimport to generate client stubs for JAX-WS webservice calls
我正在使用 wsimport 为 JAX-WS web 服务调用生成客户端存根
wsimport
has the -p
option which allows to customise name of package.
wsimport
有-p
允许自定义包名称的选项。
For eg. if the WSDL has namespace of com.abc, then you can subsitute com.abc by com.pqr by calling wsimport
with the -p com.pqr
command line.
例如。如果 WSDL 的命名空间为 com.abc,那么您可以通过命令行调用wsimport
将com.abc 替换为 com.pqr -p com.pqr
。
However, this works fine only if there is only one namespace used in the wsdl.
If there are multiple namespaces in the wsdl, is there a way to replace each of them with a different package name.
但是,只有当 wsdl 中只使用一个命名空间时,这才能正常工作。
如果 wsdl 中有多个命名空间,有没有办法用不同的包名替换它们中的每一个。
For eg. if I want namespace com.abc.s1 to be replaced by namespace com.pqr.s1 & namespace com.abc.s2 to be replaced by namespace com.pqr.s2.
例如。如果我希望命名空间 com.abc.s1 被命名空间 com.pqr.s1 替换 & 命名空间 com.abc.s2 被命名空间 com.pqr.s2 替换。
If I use wsimport -p com.pqr.s1
, it puts all the generated classes into com.pqr.s1
如果我使用wsimport -p com.pqr.s1
,它将所有生成的类放入 com.pqr.s1
Is there a way to achieve what I want?
有没有办法实现我想要的?
回答by kolossus
Generally, you use a jax-b bindings file to customize the unmarshal process for a given XSD or WSDL. The bindings language provides the <package/>
directive for the purpose of customizing the generated package of a schema.
通常,您使用 jax-b 绑定文件来自定义给定 XSD 或 WSDL 的解组过程。绑定语言提供了<package/>
用于定制模式的生成包的指令。
Given separate schemata, in separate files, you can have a composite bindings file that'll look something like this:
给定单独的模式,在单独的文件中,你可以有一个复合绑定文件,看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
jaxb:version="2.0">
<jaxb:bindings schemaLocation="Flight.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="travel.flight"/>
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="Hotel.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="travel.hotel"/>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
Where schemaLocation
will refer to the location of individual schema files, node
refers to the XML element that the binding declaration is supposed to apply to. <jaxb:package/>
will define the name of the output package.
whereschemaLocation
将指代各个模式文件的位置,node
指的是绑定声明应该应用于的 XML 元素。<jaxb:package/>
将定义输出包的名称。
You should then feed the bindings file to wsimportusing the -b
directive and you should be fine
然后你应该使用指令将绑定文件提供给wsimport-b
,你应该没问题
Reference:
参考:
回答by Glenn Van Schil
The way i did it, is by doing the following.
我这样做的方式是执行以下操作。
First create a schema.xjc file
首先创建一个schema.xjc文件
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
jaxb:version="2.0">
<jaxb:bindings schemaLocation="YOUR_URL?wsdl#types?schema1">
<jaxb:schemaBindings>
<jaxb:package name="your.package.name.schema1"/>
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="YOUR_URL??wsdl#types?schema2">
<jaxb:schemaBindings>
<jaxb:package name="your.package.name.schema2"/>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
The package name can be anything you want it to be, as long as it doesn't contain any reserved keywords in Java
包名可以是任何你想要的,只要它不包含任何 Java 中的保留关键字
Next you have to create the wsimport.bat script to generate your packaged and code at the preferred location.
接下来,您必须创建 wsimport.bat 脚本以在首选位置生成打包和代码。
cd C:\YOUR\PATH\TO\PLACE\THE\PACKAGES
wsimport -keep -verbose -b "C:\YOUR\PATH\TO\schema.xjb" YOUR_URL?wsdl
pause
If you do not want to use cd, you can put the wsimport.bat in "C:\YOUR\PATH\TO\PLACE\THE\PACKAGES"
如果不想用cd,可以把wsimport.bat放在“C:\YOUR\PATH\TO\PLACE\THE\PACKAGES”
If you run it without -keep -verbose it will only generate the packages but not the .java files.
如果你在没有 -keep -verbose 的情况下运行它,它只会生成包而不是 .java 文件。
The -b will make sure the schema.xjc is used when generating
-b 将确保在生成时使用 schema.xjc