xmlspring-bootmappingunmarshallingcastor

castors unmarshalling mapping internal classes from xml, spring-boot


I have an xml file that i map to 2 java classes using castors unmarshalling. It currently looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configurations>
    <Port>8007</Port>
    <EnableHttps>true</EnableHttps>
    <KeyStorePath>classpath:ssl-server.jks</KeyStorePath>
    <KeyPass>changeit</KeyPass>
    <TokenTtlMillis>15000</TokenTtlMillis>
  <Users>
    <Username>user1</Username>
    <Password>$2a$10$kIWaw2g1w0TC.RUtr1k3nu3PBZ9qtaLvAMhFmtbF2X8cwiXC9OXhS</Password> <!-- password -->
  </Users>
  <Users>
    <Username>user2</Username>
    <Password>$2a$10$jxGxonindJQgZjQoRxDhPewFjOo0F3ZoU8.VOFEOHTuMIJHRVTyF6</Password> <!-- password2 -->
  </Users>
</Configurations>

Now i would like to wrap my <users>in another <users>so it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configurations>
  <Port>8007</Port>
  <EnableHttps>true</EnableHttps>
  <KeyStorePath>classpath:ssl-server.jks</KeyStorePath>
  <KeyPass>changeit</KeyPass>
  <TokenTtlMillis>15000</TokenTtlMillis>
  <Users>
    <User>
      <Username>user1</Username>
      <Password>$2a$10$kIWaw2g1w0TC.RUtr1k3nu3PBZ9qtaLvAMhFmtbF2X8cwiXC9OXhS</Password> <!-- password -->
    </User>
    <User>
      <Username>user2</Username>
      <Password>$2a$10$jxGxonindJQgZjQoRxDhPewFjOo0F3ZoU8.VOFEOHTuMIJHRVTyF6</Password> <!-- password2 -->
    </User>
  </Users>
</Configurations>

How do i do this in the mapping file? Currently looks like this, with 1 class having a list of the other class

<mapping>
  <class name="com.cetrea.securityservice.bean.Configurations" auto-complete="true">
    <map-to xml="Configurations"/>
    <field name="port" type="int">
      <bind-xml name="Port" node="element"/>
    </field>
    <field name="enableHttps" type="boolean">
      <bind-xml name="EnableHttps" node="element"/>
    </field>
    <field name="keyStorePath" type="string">
      <bind-xml name="KeyStorePath" node="element"/>
    </field>
    <field name="keyPass" type="string">
      <bind-xml name="KeyPass" node="element"/>
    </field>
    <field name="TTL" type="int">
      <bind-xml name="TokenTtlMillis" node="element"/>
    </field>
    <field name="Users" collection="arraylist" type="com.cetrea.securityservice.bean.Users">
      <bind-xml name="Users"/>
    </field>
  </class>
  <class name="com.cetrea.securityservice.bean.Users" auto-complete="true">
    <map-to xml="Users"/>
    <field name="Username" type="string">
      <bind-xml name="Username" node="element"/>
    </field>
    <field name="Password" type="string">
      <bind-xml name="Password" node="element"/>
    </field>
  </class>
</mapping>

I'm thinking i need the Configurations class, to have a field that is the Users class, and then the Users class needs to have a list of users. But i cant really wrap my head around how to implement it, and what to change in my java model classes


Solution

  • I added <location="Users"> to the mapping file like this under the List. That creates a wrapper, that doesnt need to be in the model classes

    <mapping>
      <class name="com.cetrea.securityservice.bean.Configurations" auto-complete="true">
        <map-to xml="Configurations"/>
        <field name="port" type="int">
          <bind-xml name="Port" node="element"/>
        </field>
        <field name="enableHttps" type="boolean">
          <bind-xml name="EnableHttps" node="element"/>
        </field>
        <field name="keyStorePath" type="string">
          <bind-xml name="KeyStorePath" node="element"/>
        </field>
        <field name="keyPass" type="string">
          <bind-xml name="KeyPass" node="element"/>
        </field>
        <field name="TTL" type="int">
          <bind-xml name="TokenTtlMillis" node="element"/>
        </field>
        <field name="Users" collection="arraylist" type="com.cetrea.securityservice.bean.Users">
          <bind-xml name="User" location="Users"/>
        </field>
      </class>
      <class name="com.cetrea.securityservice.bean.Users" auto-complete="true">
        <map-to xml="User"/>
        <field name="Username" type="string">
          <bind-xml name="Username" node="element"/>
        </field>
        <field name="Password" type="string">
          <bind-xml name="Password" node="element"/>
        </field>
      </class>
    </mapping>