简体   繁体   English

如何生成从XSD实现可序列化的Java文件

[英]How to generate java file which implements serializable from xsd

I have to generate a class which implements serializable interface also, following is the xsd file I am using and also using maven jaxb2 plugin 我必须生成一个也实现可序列化接口的类,以下是我正在使用的xsd文件以及maven jaxb2插件

This is my .xsd file 这是我的.xsd文件

<?xml version="1.0" encoding="windows-1252" ?>

<xsd:complexType name="Status">
    <xsd:sequence>
        <xsd:element name="startTime" type="xsd:double" minOccurs="0" />
        <xsd:element name="endTime" type="xsd:double" minOccurs="0" />
    </xsd:sequence>
</xsd:complexType>

My JAXB2 plugin 我的JAXB2 plugin

 <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.3</version>
            <executions>
                <execution>
                    <id>status</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <forceRegenerate>true</forceRegenerate>
                        <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                        <schemaIncludes>
                            <include>Status.xsd</include>
                        </schemaIncludes>
                        <generatePackage>com.test.model</generatePackage>
                        <args>
                            <arg>-XtoString</arg>
                            <arg>-Xcopyable</arg>
                            <arg>-Xequals</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics</artifactId>
                                <version>0.6.4</version>
                            </plugin>
                        </plugins>
                    </configuration>
                </execution>

            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.datastax.cassandra</groupId>
                    <artifactId>cassandra-driver-core</artifactId>
                    <version>3.1.3</version>
                </dependency>
                <dependency>
                    <groupId>com.datastax.cassandra</groupId>
                    <artifactId>cassandra-driver-mapping</artifactId>
                    <version>3.1.3</version>
                </dependency>
            </dependencies>
        </plugin>

here generated java class implementing Cloneable, CopyTo, Equals, ToString interfaces I tried with <arg>-Xserializable</arg> but it didn't work, I need this generated class should also implement Serializable interface 这里生成了实现Cloneable, CopyTo, Equals, ToString interfaces Java类Cloneable, CopyTo, Equals, ToString interfaces我尝试使用<arg>-Xserializable</arg>但是它没有用,我需要这个生成的类也应该实现Serializable interface

I used jaxb2-maven-plugin like this: 我像这样使用jaxb2-maven-plugin:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extension>true</extension>
                <arguments>-XtoString -Xcopyable</arguments>
                <outputDirectory>target/generated-sources/</outputDirectory>
                <schemaDirectory>src/main/resources</schemaDirectory>
                <bindingDirectory>src/main/resources</bindingDirectory>
                <bindingFiles>binding.xml</bindingFiles>
            </configuration>
        </plugin>

The binding.xml: binding.xml:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">
<jaxb:globalBindings generateIsSetMethod="true">
    <xjc:serializable uid="12343" />
</jaxb:globalBindings>

With this dependency: 具有这种依赖性:

<dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics</artifactId>
</dependency>

I got solution Modified xsd:schema declaration to 我将解决方案修改为xsd:schema declaration to

<xsd:schema xmlns:xsd="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"
       jaxb:extensionBindingPrefixes="xjc"
       jaxb:version="1.0">
 <xsd:annotation>
   <xsd:appinfo>
    <jaxb:globalBindings generateIsSetMethod="true">
      <xjc:serializable uid="12343"/>
    </jaxb:globalBindings>
  </xsd:appinfo>
</xsd:annotation>

then added jaxp.properties (if it doesn't exist) under path/to/jdk1.8.0/jre/lib and then write this line in it: 然后在path/to/jdk1.8.0/jre/lib下添加jaxp.properties (如果不存在),然后在其中写入以下行:

javax.xml.accessExternalSchema = all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM