是否可以使用CXF生成可序列化的类?

时间:2011-08-10 10:47:47

标签: java serialization cxf

我正在使用Apache CXF从WSDL文件生成类,但它们没有实现将对象放入JMS队列所需的Serializable。是否可以这样做或者我必须将生成的类转换为我自己的类并发送它们吗?

3 个答案:

答案 0 :(得分:14)

我自己找到了解决方案,所以如果有人在将来需要它,那么这就是:

<强> 1. add this plugin to pom.xml

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                <wsdlRoot>${basedir}/src/main/resources</wsdlRoot>
                <includes>
                    <include>*Service.wsdl</include>
                </includes>
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>${basedir}/src/main/resources/your_wsdl.wsdl</wsdl>
                        <extraargs>
                            <extraarg>-client</extraarg>
                            <extraarg>-impl</extraarg>
                            <extraarg>-server</extraarg>
                            <extraarg>-verbose</extraarg>
                            <extraarg>-validate</extraarg>
                        </extraargs>
                        <bindingFiles>
                            <bindingFile>${basedir}/src/main/resources/binding.xml</bindingFile>
                        </bindingFiles>
                    </wsdlOption>
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<强> 2. write a custom binding (binding.xml)

<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings wsdlLocation="Send.wsdl"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <jaxws:bindings
        node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://wsdl/SendService.wsdl']">
        <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <jxb:serializable uid="11082011"/>
        </jxb:globalBindings>
    </jaxws:bindings>
</jaxws:bindings>

<强> 3. run 'mvn generate-sources'

答案 1 :(得分:5)

使用&lt; jaxws:bindings&gt;绑定文件只有当wsdl具有单个模式时,root元素才能使用jaxb:globalBindings。问题是jaxb:globalBindings是全局的,因此不能多次指定它。

如果是这种情况,您可以将以下绑定文件与jaxb:bindings一起使用:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
    <jaxb:globalBindings>
        <jaxb:serializable uid="1"/>
    </jaxb:globalBindings>
</jaxb:bindings>

答案 2 :(得分:4)

更简单的 bindings.xml 版本,可确保所有生成的文件都实现Serializable:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings>
                <xjc:serializable />
            </jaxb:globalBindings>
        </xs:appinfo>
    </xs:annotation>
</xs:schema>