1

Below is a test class. I am using spring boot 3 with Java 17 and JAXB. Controller is Rest controller. O/P from the endpoint is expected to be amount_currencyCode. However, the adapter is ignored by spring and unwanted xml is o/p'ed.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TestClass implements Serializable {
  
    @XmlJavaTypeAdapter(MonetaryAmountAdapter.class)
    @XmlElement(name = "taxTotal")
    private MonetaryAmount taxTotal;
}

Adapter class-

public class MonetaryAmountAdapter extends XmlAdapter<String , MonetaryAmount> {

  @Override
  public MonetaryAmount unmarshal(String value) throws Exception { 
    String[] split = value.split("_");
    if(split.length==2)
      return MoneyUtil.monetaryAmount(new BigDecimal(split[0]), split[1]);
    return MoneyUtil.monetaryAmount(new BigDecimal(split[0]), "USD");
  }

  /**
   * Converts MonetaryAmount into string format: amount_currencyCode
   */
  @Override
  public String marshal(MonetaryAmount value) throws Exception {
      if (!value.getCurrency().getCurrencyCode().equals("USD"))
          return value.getNumber().toString()+ "_" +value.getCurrency().getCurrencyCode(); 
      else
          return value.getNumber().toString();
  }  
}

Tried to add various jakarta/jaxb related dependencies. Annotated the Adapters with @configurable/@component. Nothing seem to have worked. Previous application which we are migrating has many users and can't change the xml rest contract. Old app was working probably because of spring MVC. Newer app is Java 17 with spring boot 3 and having Rest based APIs.

Nitin
  • 11
  • 1

0 Answers0