jaxbmaven-jaxb2-pluginjaxb2-maven-plugin

why is jaxb creating an adapter for converting datatypes


I have a project that I am upgrading to jakarta 10. The pom looks something like this

 <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
     http://maven.apache.org/xsd/maven-4.0.0.xsd">

     <modelVersion>4.0.0</modelVersion>

 <parent>
   <artifactId>xxxx</artifactId>
   <groupId>xx.xxx.xxx</groupId>
   <version>1.1-SNAPSHOT</version>
 </parent>

 <artifactId>xx.xx.xxxx</artifactId>
 <packaging>jar</packaging>

 <dependencies>
   .......
 <dependency>
   <groupId>jakarta.validation</groupId>
   <artifactId>jakarta.validation-api</artifactId>
  <version>${validation-api.version}</version>
</dependency>

<dependency>
  <groupId>jakarta.xml.bind</groupId>
  <artifactId>jakarta.xml.bind-api</artifactId>
  <version>${jaxb-api.version}</version>
</dependency>

<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>4.0.3</version>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.jvnet.jaxb</groupId>
  <artifactId>jaxb-plugins</artifactId>
  <version>${jaxb2-plugins.version}</version>
</dependency>

<dependency>
  <groupId>org.jvnet.jaxb</groupId>
  <artifactId>jaxb-plugins-runtime</artifactId>
  <version>${jaxb2-plugins.version}</version>
</dependency>

<dependency>
  <groupId>org.jvnet.jaxb</groupId>
  <artifactId>jaxb-plugin-annotate</artifactId>
  <version>${jaxb2-plugins.version}</version>
</dependency>


<dependency>
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-module-jaxb-annotations</artifactId>
  <version>${jackson.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.wiremock</groupId>
  <artifactId>wiremock-standalone</artifactId>
  <scope>test</scope>
</dependency>

</dependencies>

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
          <id>partaggregat-v2-jaxb</id>
          <phase>process-resources</phase>
          <goals>
            <goal>xjc</goal>
          </goals>
          <configuration>
            <sources>
              <source>${basedir}/src/main/resources/xsd</source>
            </sources>
            <xjbSources>
              <xjbSource>${basedir}/src/main/resources/xjb/bindings.xjb</xjbSource>
            </xjbSources>
              <arguments>
                <argument>-Xfluent-api</argument>
                <argument>-Xannotate</argument>
                <argument>-Xequals</argument>
                <argument>-XhashCode</argument>
              </arguments>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.glassfish.jaxb</groupId>
          <artifactId>jaxb-xjc</artifactId>
          <version>4.0.4</version>
        </dependency>
        <dependency>
          <groupId>jakarta.xml.bind</groupId>
          <artifactId>jakarta.xml.bind-api</artifactId>
          <version>${jaxb-api.version}</version>
        </dependency>
        <dependency>
          <groupId>com.sun.xml.bind</groupId>
          <artifactId>jaxb-impl</artifactId>
          <version>4.0.4</version>
        </dependency>
        <dependency>
          <groupId>jakarta.activation</groupId>
          <artifactId>jakarta.activation-api</artifactId>
          <version>2.1.2</version>
        </dependency>
        <dependency>
          <groupId>org.jvnet.jaxb</groupId>
          <artifactId>jaxb-plugins</artifactId>
          <version>4.0.3</version>
        </dependency>
        <dependency>
          <groupId>org.jvnet.jaxb</groupId>
          <artifactId>jaxb-plugin-annotate</artifactId>
          <version>4.0.3</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</pluginManagement>

<plugins>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
  </plugin>


</plugins>

and the binding

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:annox="urn:jaxb.jvnet.org:annox"
           jaxb:extensionBindingPrefixes="xjc annox"
           xsi:schemaLocation="https://jakarta.ee/xml/ns/jaxb 
 https://jakarta.ee/xml/ns/jaxb/bindingschema_3_0.xsd"
           version="3.0">

<jaxb:globalBindings typesafeEnumMaxMembers="600">
  <jaxb:serializable uid="1" />
  <jaxb:javaType name="java.time.Year" xmlType="xs:gYear"        
  parseMethod="com.somewhere.someotherstuff.client.utils.JsonYearAdapter" />
<jaxb:javaType name="java.time.LocalDate" xmlType="xs:date"                   
    parseMethod="com.somewhere.someotherstuff.client.utils.JsonLocalDateAdapter" />
<jaxb:javaType name="java.time.LocalDateTime" xmlType="xs:dateTime"                   
  parseMethod="com.somewhere.someotherstuff.client.utils.JsonLocalDateTimeAdapter" />
</jaxb:globalBindings>

<jaxb:bindings schemaLocation="../xsd/com.someplace.someotherthing.somename.v2.2.xsd" 
   node="/xs:schema">
  <jaxb:bindings>
    <jaxb:schemaBindings>
      <jaxb:package name="com.someplace.someotherthing.somename.v2" />
    </jaxb:schemaBindings>
  </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

I noticed that three classes (Adapter1, Adapter2 and Adapter3) corresponding to the three javatypes where created in the target folder under the org.w3._2001.xmlschema pacakge. To fix this I added the solution mentioned in this question which worked.

The problem here is that with jaxb version2 the three Adapter1,2,3 classes where not created and I do not understand why they are created when I have passed my own adapter classes in the binding.xjb file.

The classes look like this

package com.someplace.someotherthing.somename.v2;

import java.time.LocalDateTime;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;

public class Adapter1
 extends XmlAdapter<String, LocalDateTime>
{


 public LocalDateTime unmarshal(String value) {
     return 
   (com.somewhere.someotherstuff.client.utils.JsonLocalDateTimeAdapter(value));
}

public String marshal(LocalDateTime value) {
     if (value == null) {
        return null;
    }
    return value.toString();
 }
}

Why is jaxb generating theseAdapter classes?

cheers

es


Solution

  • The problem is you seem to use jaxb:javaType instead of xjc:javaType

    Instead of using the following syntax

    <jaxb:javaType name="java.time.Year" xmlType="xs:gYear"        
      parseMethod="com.somewhere.someotherstuff.client.utils.JsonYearAdapter" />
    <jaxb:javaType name="java.time.LocalDate" xmlType="xs:date"                   
        parseMethod="com.somewhere.someotherstuff.client.utils.JsonLocalDateAdapter" />
    <jaxb:javaType name="java.time.LocalDateTime" xmlType="xs:dateTime"                   
      parseMethod="com.somewhere.someotherstuff.client.utils.JsonLocalDateTimeAdapter" />
    

    You should try something like

    <xjc:javaType name="java.time.Year" xmlType="xs:gYear"
        adapter="com.somewhere.someotherstuff.client.utils.JsonYearAdapter" />
    <xjc:javaType name="java.time.LocalDate" xmlType="xs:date"
        adapter="com.somewhere.someotherstuff.client.utils.JsonLocalDateAdapter" />
    <xjc:javaType name="java.time.LocalDateTime" xmlType="xs:dateTime"
        adapter="com.somewhere.someotherstuff.client.utils.JsonLocalDateTimeAdapter" />
    

    With xjc namespace as follow : xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"