Castors 从 XML、spring-boot 解组映射内部类



我有一个xml文件,我使用castors解组映射到2个java类。它目前看起来像这样:

<?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>
现在我想用

另一个<users>包裹我的<users>,所以它看起来像这样:

<?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>

如何在映射文件中执行此操作?目前看起来像这样,其中 1 个类具有另一个类的列表

<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>

我想我需要配置类,有一个字段是用户类,然后用户类需要有一个用户列表。但是我真的无法弄清楚如何实现它,以及在我的java模型类中要更改什么

我像

这样在列表下添加了<location="Users">到映射文件中。这将创建一个包装器,不需要在模型类中

<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>

相关内容

  • 没有找到相关文章

最新更新