自定义 Java 包 JAXB wsimport
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25932578/
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
Customizing Java packages JAXB wsimport
提问by maqjav
I'm trying to generate a client with maven and jaxb from a wsdl file with 2 schemas inside and some elements with the same name from different schemas
我正在尝试从 wsdl 文件中生成一个带有 maven 和 jaxb 的客户端,其中包含 2 个模式,以及一些来自不同模式的同名元素
When I try to execute the compilation I'm getting the next error:
当我尝试执行编译时,出现下一个错误:
Two declarations cause a collision in the ObjectFactory class.
WSDL schemas:
WSDL 模式:
<wsdl:types>
<schema targetNamespace="http://ws.services" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
<schema targetNamespace="http://ws.models" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
</wsdl:types>
I tried renaming the elements that produce the error, but then my spring client receive the correct SOAP message, but it doesn't populate the response object properly (all its attributes are null). I guess the problem might come from renaming the response classes, so that's why I'm trying to generate different packages keeping the original name of all the classes.
我尝试重命名产生错误的元素,但随后我的 spring 客户端收到了正确的 SOAP 消息,但它没有正确填充响应对象(其所有属性均为空)。我想问题可能来自重命名响应类,所以这就是为什么我试图生成不同的包并保留所有类的原始名称。
In order to do that, I wrote the next bindings file but I don't know what I'm doing wrong that it is not working.
为了做到这一点,我编写了下一个绑定文件,但我不知道我做错了什么,它不起作用。
bindings.xmlfile:
bindings.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema1"
node="/xs:schema[@targetNamespace='http://ws.services']">
<jaxb:schemaBindings>
<jaxb:package name="package1" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema2"
node="/xs:schema[@targetNamespace='http://ws.models']">
<jaxb:schemaBindings>
<jaxb:package name="package2" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
My configuration part in the maven file is the next, just in case it is useful:
我在 Maven 文件中的配置部分是下一个,以防万一它有用:
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlLocation>wsdl/mywsdl.wsdl</wsdlLocation>
<wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>mywsdl.wsdl</wsdlFile>
</wsdlFiles>
<bindingDirectory>src/main/resources/wsdl</bindingDirectory>
<bindingFiles>
<bindingFile>bindings.xml</bindingFile>
</bindingFiles>
<packageName>original.package</packageName>
<sourceDestDir>${basedir}/src/main/java</sourceDestDir>
</configuration>
When I compile with this bindings files, the same error appears. So I think that maybe it isn't right.
当我用这个绑定文件编译时,出现同样的错误。所以我认为这可能是不对的。
Do you find any mistakes?
你有没有发现任何错误?
Thanks.
谢谢。
回答by headcr4sh
From my experience it is best to create 2 binding files (one for each WSDL file). Update your pom.xml accordingly and make sure that the root element of the binding files is jaxws:bindings (and not jaxb:bindings!)
根据我的经验,最好创建 2 个绑定文件(每个 WSDL 文件一个)。相应地更新 pom.xml 并确保绑定文件的根元素是 jaxws:bindings(而不是 jaxb:bindings!)
Some hints:
一些提示:
- Make sure to set the "wsdlLocation" attribute correctly! It must point to the WSDL file using a relative path!
- The jaxws:package determines the package that will be used for the generated service classes. (the stuff annotated with @WebService)
- Enable or disable wrapperStyle and asyncMapping as you wish. ;-)
- 确保正确设置“wsdlLocation”属性!它必须使用相对路径指向 WSDL 文件!
- jaxws:package 确定将用于生成的服务类的包。(用@WebService 注释的东西)
- 根据需要启用或禁用 wrapperStyle 和 asyncMapping。;-)
Example binding file for "package1":
“package1”的示例绑定文件:
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
wsdlLocation="mywsdl.wsdl"
version="2.0">
<jaxws:package name="package1"/>
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
<jaxws:enableAsyncMapping>true</jaxws:enableAsyncMapping>
<jaxws:bindings node="//wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://ws.services']">
<jaxb:schemaBindings>
<jaxb:package name="package1"/>
</jaxb:schemaBindings>
</jaxws:bindings>
</jaxws:bindings>