简体   繁体   中英

Error while mapping xml to java using jackson: com.fasterxml.jackson.databind.exc.MismatchedInputException

I have an xml file that I have to parse and map to a Java class:

<udm.DeployedApplication id="ID" token="TOKEN" created-by="AUTHOR" created-at="2018-12-10T10:02:36.264+0000" last-modified-by="AUTHOR" last-modified-at="2019-05-21T18:16:07.492+0000">
    <version ref="REF"/>
    <environment ref="REF2"/>
    <deployeds>
        <ci ref="ref3"/>
    </deployeds>
    <orchestrator>
        <value>value1</value>
        <value>value2</value>
        <value>value3</value>
    </orchestrator>
    <optimizePlan>true</optimizePlan>
    <boundConfigurationItems/>
    <unresolvedPlaceholders/>
    <undeployDependencies>false</undeployDependencies>
    <bpcForceUndeployment>false</bpcForceUndeployment>
    <enableAutomaticOrchestrators>true</enableAutomaticOrchestrators>
    <hotDeploy>false</hotDeploy>
</udm.DeployedApplication>

XlDeployCi.java:

@JsonIgnoreProperties(ignoreUnknown = true)
public class XlDeployCi {
    @JacksonXmlProperty(localName = "id")
    private String id;
    @JacksonXmlProperty(localName = "token")
    private String token;
    @JacksonXmlProperty(localName = "created-by")
    private String createdBy;
    @JacksonXmlProperty(localName = "created-at")
    private Timestamp createdAt;
    @JacksonXmlProperty(localName = "last-modified-by")
    private String modifiedBy;
    @JacksonXmlProperty(localName = "last-modified-at")
    private Timestamp modifiedAt;

    @JacksonXmlProperty(localName = "version")
    private XlDeployRef version;
    @JacksonXmlProperty(localName = "environment")
    private XlDeployRef environment;
    @JacksonXmlProperty(localName = "deployeds")
    private List<XlDeployRef> deployeds;

    @JacksonXmlProperty(localName = "orchestrator")
    private XlDeployOrchestrator orchestrator;

    @JacksonXmlProperty(localName = "boundConfigurationItems")
    private String boundConfigurationItems;
    @JacksonXmlProperty(localName = "unresolvedPlaceholders")
    private String unresolvedPlaceholders;
    @JacksonXmlProperty(localName = "optimizePlan")
    private Boolean optimizePlan;
    @JacksonXmlProperty(localName = "undeployDependencies")
    private Boolean undeployDependencies;
    @JacksonXmlProperty(localName = "bpcForceUndeployment")
    private Boolean bpcForceUndeployment;
    @JacksonXmlProperty(localName = "enableAutomaticOrchestrators")
    private Boolean enableAutomaticOrchestrators;
    @JacksonXmlProperty(localName = "hotDeploy")
    private Boolean hotDeploy;
}

XlDeployRef.java:

@JsonIgnoreProperties(ignoreUnknown = true)
public class XlDeployRef {
    @JacksonXmlProperty(localName = "ref")
    private String ref;

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }
}

XlDeployOrchestrator.java:

@JsonIgnoreProperties(ignoreUnknown = true)
public class XlDeployOrchestrator {
    @JacksonXmlProperty(localName = "value")
    private ArrayList <String> value;
}

TestXml.java:

public class TestXml {

    private static final String FILE_PATH = "src/main/resources/xmlRest.txt";

    @Test
    public void testXml() throws JsonParseException, JsonMappingException, IOException {
        String xmlFile = readFile(FILE_PATH, Charset.defaultCharset());
        XmlMapper xmlMapper = new XmlMapper();

        xmlMapper.setDefaultUseWrapper(false);
        xmlMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        List<XlDeployCi> list = xmlMapper.readValue(xmlFile, new TypeReference<List<XlDeployCi>>() {});

        list.forEach(System.out::println);
    }

    private static String readFile(String path, Charset encoding) throws IOException {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return new String(encoded, encoding);
    }
}

The error I get :

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of package.XlDeployCi (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('true')

EDIT

I tried few things, and found something strange, I changed my xml file to :

<udm.DeployedApplication id="Environments/UAT/Env/UAT-DCS_APL/dcs-application" token="b3dd242b-66ff-4548-81b1-062c8a1152be" created-by="ext.pcuciniello" created-at="2018-12-10T10:02:36.264+0000" last-modified-by="ext.pcuciniello" last-modified-at="2019-05-21T18:16:07.492+0000">
    <version ref="Applications/Shipping/Java/DCS/dcs-application/dcs1.8.5-20190521"/>
    <environment ref="Environments/UAT/Env/UAT-DCS_APL"/>
    <deployeds>
        <ci ref="Infrastructure/UAT/WEBLO/UAT_DCS_APL_DOMAIN/DCS_UAT_CLUSTER/dcs-application"/>
    </deployeds>
    <hotDeploy>true</hotDeploy>
</udm.DeployedApplication>

Then, I changed the XlDeployCi.java file:

@JsonIgnoreProperties(ignoreUnknown = true)
public class XlDeployCi {

    @JacksonXmlProperty(localName = "id")
    private String id;
    @JacksonXmlProperty(localName = "token")
    private String token;
    @JacksonXmlProperty(localName = "created-by")
    private String createdBy;
    @JacksonXmlProperty(localName = "created-at")
    private Timestamp createdAt;
    @JacksonXmlProperty(localName = "last-modified-by")
    private String modifiedBy;
    @JacksonXmlProperty(localName = "last-modified-at")
    private Timestamp modifiedAt;

    @JacksonXmlProperty(localName = "version")
    private XlDeployRef version;
    @JacksonXmlProperty(localName = "environment")
    private XlDeployRef environment;
    @JacksonXmlProperty(localName = "deployeds")
    private List<XlDeployRef> deployeds;

    @JacksonXmlProperty(localName = "hotDeploy")
    public String hotDeploy;

    @Override
    public String toString() {
        return "XlDeployCi [id=" + id + ", token=" + token + ", createdBy=" + createdBy + ", createdAt=" + createdAt
                + ", modifiedBy=" + modifiedBy + ", modifiedAt=" + modifiedAt + ", version=" + version
                + ", environment=" + environment + ", deployeds=" + deployeds + ", hotDeploy=" + hotDeploy + "]";
    }

}

The error I get :

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of package.XlDeployCi (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('false')

But , If I remove the line <hotDeploy>false</hotDeploy> , it works correctly.

It's seems, I can't do something like <X>Y</Y> , I can only do stuff like <XY="Z"/> .

I found the solution:
My mistake, (a terrible one), was, that I forgot to change the type of the mapping class for the method xmlMapper.readValue(String content, Class<T> valueType) .

Indeed, I set the class of the mapping object as new TypeReference<List<XlDeployCi>>() {} . You should do that to get a list, but actually, I only needed 1 object.

I leave this answer, in case someone makes the same mistake as me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM