How to generate XSDs from java beans having java.time.LocalDate?

Viewed 984

I'm trying to use jaxb2-maven-plugin to generate xsd from java beans.

But I constantly get the following error:

java.time.LocalDate is a non-static inner class, and JAXB can't handle those.
    this problem is related to the following location:
        at java.time.LocalDate (Unknown Source)

The bean is like:

public class MyBean {
    private LocalDate date;
}

I therefore created a jaxb mapping file and an adapter converting between java.time.LocalDate and xs:date. But the error just remains the same.

 public class LocalDateAdapter {
    private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");

    public static LocalDate unmarshal(String v) throws Exception {
        return fmt.parseLocalDate(v);
    }

    public static String marshal(LocalDate v) throws Exception {
        return v.toString("yyyyMMdd");
    }
 }

src/main/resources/xsdbindings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
          version="2.1">
    <globalBindings>
        <javaType name="java.time.LocalDate" xmlType="xs:date"
                  parseMethod="my.path.LocalDateAdapter.unmarshal"
                  printMethod="my.path.LocalDateAdapter.marshal"
        />
    </globalBindings>
</bindings>

maven pom.xml

...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <id>schemagen</id>
                        <goals>
                            <goal>schemagen</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <bindingFiles>xsdbindings.jaxb</bindingFiles>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
0 Answers
Related