Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使jaxb插件使用OffsetDateTime_Java_Java Time_Maven Jaxb2 Plugin_Jaxb2 Maven Plugin_Cxf Codegen Plugin - Fatal编程技术网

Java 如何使jaxb插件使用OffsetDateTime

Java 如何使jaxb插件使用OffsetDateTime,java,java-time,maven-jaxb2-plugin,jaxb2-maven-plugin,cxf-codegen-plugin,Java,Java Time,Maven Jaxb2 Plugin,Jaxb2 Maven Plugin,Cxf Codegen Plugin,我们有一个带有xs:dateTime字段的xsd。这是我们的内部API,我们可以保证始终包含偏移数据,因此它与ISO-8601兼容。例如: 2016-01-01T00:00:00.000+01:00 目前,jaxb2插件将xs:dateTime映射到XMLGregorianCalendar类型的字段。如何配置插件,使其使用OffsetDateTime 我不在乎解决方案是针对maven-jaxb2-plugin、jaxb2-maven-plugin还是cxf-codegen-plugin,我们将使

我们有一个带有
xs:dateTime
字段的xsd。这是我们的内部API,我们可以保证始终包含偏移数据,因此它与ISO-8601兼容。例如:

2016-01-01T00:00:00.000+01:00

目前,jaxb2插件将
xs:dateTime
映射到
XMLGregorianCalendar
类型的字段。如何配置插件,使其使用
OffsetDateTime


我不在乎解决方案是针对
maven-jaxb2-plugin
jaxb2-maven-plugin
还是
cxf-codegen-plugin
,我们将使用任何可行的方法。

您可以将
jaxb2-maven-plugin
jaxb绑定
文件一起使用

首先,我创建了一个
odt.xsd
文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="teste" type="Teste" />
  <xsd:complexType name="Teste">
    <xsd:sequence>
      <xsd:element name="date" type="xsd:dateTime" minOccurs="1"
        maxOccurs="1" nillable="false"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
该文件引用了
xsd.test.OffsetDateTimeAdapter
类以及将
OffsetDateTime
从字符串转换为字符串的相应方法,因此我还创建了它:

package xsd.test;

import java.time.OffsetDateTime;

public class OffsetDateTimeAdapter {

    public static OffsetDateTime parse(String value) {
        return OffsetDateTime.parse(value);
    }

    public static String print(OffsetDateTime value) {
        return value.toString();
    }
}
然后,在
pom.xml
中,我添加了插件的配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>xjc</id>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- The package of your generated sources -->
        <packageName>xsd.test</packageName>
        <sources>
            <source>src/main/resources/odt.xsd</source>
        </sources>
        <xjbSources>
            <xjbSource>src/main/resources/jaxb-bindings.xjb</xjbSource>
        </xjbSources>
    </configuration>
</plugin>
这样,使用自动生成的
Adapter1
(内部使用上面创建的
xsd.test.OffsetDateTimeAdapter
类),将
date
字段映射到
OffsetDateTime
。从xml解析日期的示例:

ObjectFactory f = new ObjectFactory();
JAXBContext context = JAXBContext.newInstance("xsd.test");
Unmarshaller unmarshaller = context.createUnmarshaller();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:teste xmlns:ns2=\"xsd.test\"><date>2016-01-01T00:00+01:00</date></ns2:teste>";
JAXBElement<Teste> jaxElement = unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(xml.getBytes())), Teste.class);
OffsetDateTime odt = jaxElement.getValue().getDate();
System.out.println(odt); // 2016-01-01T00:00+01:00
这将在
xsd.test
包的
src/main/java
目录中生成类。

解决了这个问题

该库围绕JDK8
OffsetXXX
date/time类展开,因为它们是XML模式类型
date
dateTime
time
的(唯一)自然等价物。不幸的是,这场比赛没有一对一的比赛

这样使用:

添加依赖项:

默认情况下,JAXB2 Maven插件将拾取
src/main/xjb/
文件夹中的所有
.xjb
文件,因此您不必在插件的配置中指定上述文件


该项目提供了有关如何使用该库的更多信息。

谢谢,这非常有效。唯一的缺点是必须枚举
xjb
中的所有
xsd:dateTime
元素。但这是一个小问题。@LustigerAstronaut不客气,很乐意帮忙!也许您可以将绑定更改为
node=“//xsd:element[@type='xsd:dateTime']”
(不过我还没有测试过)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Teste", propOrder = {
    "date"
})
public class Teste {

    @XmlElement(required = true, type = String.class)
    @XmlJavaTypeAdapter(Adapter1 .class)
    @XmlSchemaType(name = "dateTime")
    protected OffsetDateTime date;
    // getter and setter
}
ObjectFactory f = new ObjectFactory();
JAXBContext context = JAXBContext.newInstance("xsd.test");
Unmarshaller unmarshaller = context.createUnmarshaller();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:teste xmlns:ns2=\"xsd.test\"><date>2016-01-01T00:00+01:00</date></ns2:teste>";
JAXBElement<Teste> jaxElement = unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(xml.getBytes())), Teste.class);
OffsetDateTime odt = jaxElement.getValue().getDate();
System.out.println(odt); // 2016-01-01T00:00+01:00
xjc src/main/resources/odt.xsd -d src/main/java/ -p xsd.test -b src/main/resources/jaxb-bindings.xjb
<dependency>
    <groupId>com.addicticks.oss</groupId>
    <artifactId>jtexttime</artifactId>
    <version> ... latest ...</version>
</dependency>