Craftoncu
Craftoncu

Reputation: 21

Jakarta XML Adapter with API object

I want to resolve the xlink links in my xml for a better java structure. But there is no way to have custom objects in a xml converter

I have following xml:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <product>
        <id><![CDATA[1487]]></id>
            <id_manufacturer xlink:href="https://redacted.xy/api/manufacturers/123"><![CDATA[91]]></id_manufacturer>
    </product>
</prestashop>


public class Prestashop {
private String id;
private Manufacturer manufacturer;
}  

public class Manufacturer { 
private String id; 
private String name; 
etc... 
}  

public class ManufacturerAdapter extends XmlAdapter<Manufacturer, Map<String, String>> {  

    private final PrestaShopAPI prestaShopAPI;  

    public ManufacturerAdapter(PrestaShopAPI prestaShopAPI) {     
         this.prestaShopAPI = prestaShopAPI; 
    }  

    @Override 
    public Manufacturer unmarshal(PrestaShopWrapper wrapper) throws Exception {     
         Map<String, String> map = new HashMap<>(); 
    }  

    @Override public Map<String, String> marshal(Manufacturer v) {     
       return null; 
    }
}

Any ideas how to solve this?

I tried using custom XmlAdapter, but I have to access the api object in the adapter for the xlink resolution.

Defining a custom constructor in the adapter results in a NoSuchMethodException because Jakarta need a noArgConstructor.

I tried setting the adapter manually in the marshaller, but this wont work too.

Upvotes: 1

Views: 22

Answers (0)

Related Questions