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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7009586/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:15:57  来源:igfitidea点击:

Is it possible to generate serializable classes with CXF?

javaserializationcxf

提问by user219882

I'm using Apache CXF to generate classes from a WSDL file but they don't implement Serializable which is needed to put an object into a JMS queue. Is it possible to do this or do I have to convert the generated classes into my own and send those?

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

回答by user219882

I found the solution myself so here it is if anybody needs it in the future:

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

1. add this plugin to pom.xml

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)

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'

3. run 'mvn generate-sources'

回答by Constantino Cronemberger

Binding files with <jaxws:bindings> root element work with jaxb:globalBindings only if your wsdl has a single schema. The problem is that jaxb:globalBindings is global so it can not be specified more than once.

仅当 wsdl 具有单个模式时,使用 <jaxws:bindings> 根元素的绑定文件才能与 jaxb:globalBindings 一起使用。问题是 jaxb:globalBindings 是全局的,因此不能多次指定。

If that is your case you can use the following bindings file with jaxb:bindings:

如果是这种情况,您可以将以下绑定文件与 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>

回答by Marcin

A simpler version of bindings.xml, which make sure that all generated files are implementing Serializable:

一个更简单的bindings.xml版本,它确保所有生成的文件都实现了可序列化:

<?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>