У меня есть XML-файл, который мне нужно проанализировать и сопоставить с классом Java:
<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);
}
}
Я получаю ошибку:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Невозможно создать экземпляр package.XlDeployCi
(хотя хотя бы один создательсуществует): нет конструктора / фабричного аргумента String-аргумента для десериализации из строкового значения ('true')
EDIT
Я пробовал несколько вещей и нашел что-то странное,Я изменил свой XML-файл на:
<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>
Затем я изменил 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 = "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 + "]";
}
}
Ошибка, которую я получаю:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Невозможно создать экземпляр package.XlDeployCi
(хотя существует хотя бы один создатель): нет конструктора с аргументом строки / фабрики метod для десериализации из строкового значения ('false')
Но , если я удалю строку <hotDeploy>false</hotDeploy>
, она будет работать правильно.
Кажется, я не могу сделать что-то вроде <X>Y</Y>
, я могу делать только такие вещи, как <X Y="Z"/>
.