gpt4 book ai didi

java - RestTemplate XML 在带有属性的空标签上反序列化 null

转载 作者:行者123 更新时间:2023-12-02 01:37:18 28 4
gpt4 key购买 nike

当值位于属性中并且 my 标记除了属性之外没有任何值时,我有以下响应(级别和 my 可以有多个值)

<main>
<level id="1">
<my id="111" amount="100000.00"/>
</level>
<level id="2">
<my id="121" amount="5000.00"/>
</level>
<level id="3">
<my id="122" amount="500.00"/>
</level>
</main>

尝试反序列化时,my 为 null

  ResponseEntity<RequestVO> jackpotFeederResponse = restTemplate.exchange(uriBuilder.build(), HttpMethod.GET,
HttpEntity.EMPTY, RequestVO.class);

我尝试添加不同的DeserializationFeature,但失败了

            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, true);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
converter.setObjectMapper(objectMapper);
restTemplate.getMessageConverters().add(converter);

我的 POJO:

@XmlRootElement(name = "main", namespace = "")
@Data
public class RequestVO implements Serializable {

private static final long serialVersionUID = -1382015668147149795L;

@JacksonXmlProperty(localName = "level")
ArrayList<LevelVO> levelList;
}


@XmlRootElement(name = "level", namespace = "")
@Data
public class LevelVO implements Serializable {

private static final long serialVersionUID = -1382015668147149795L;

@JacksonXmlProperty(localName = "id", isAttribute = true)
String id;
@JacksonXmlProperty(localName = "my")
ArrayList<MyVO> myList;


@XmlRootElement(name = "my", namespace = "")
@Data
public class MyVO implements Serializable {

private static final long serialVersionUID = -1382015668147149795L;

@JacksonXmlProperty(localName = "id", isAttribute = true)
String id;

最佳答案

首先,您应该在集合类型上使用@JacksonXmlElementWrapper(useWrapping = false)。接下来尝试不要将 JAXB 注释与 Jackson 注释混合。因此只需使用@JacksonXmlRootElement 而不是@XmlRootElement。

您的 XML POJO 应如下所示:

@JacksonXmlRootElement(localName = "main")
@Data
public class RequestVO implements Serializable {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "level")
ArrayList<LevelVO> levelList;
}


@JacksonXmlRootElement(localName = "level")
@Data
public class LevelVO implements Serializable {
@JacksonXmlProperty(localName = "id", isAttribute = true)
String id;

@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "my")
ArrayList<MyVO> myList;
}

@JacksonXmlRootElement(localName = "my")
@Data
public class MyVO implements Serializable {
@JacksonXmlProperty(localName = "id", isAttribute = true)
String id;

@JacksonXmlProperty(localName = "amount", isAttribute = true)
String amount;
}

如果您确实想坚持使用@XmlRootElement Annotation。您可能还必须将 JaxbAnnotationModule 注册到 Jacksons XmlMapper:

JaxbAnnotationModule module = new JaxbAnnotationModule();
xmlMapper.registerModule(module);

关于java - RestTemplate XML 在带有属性的空标签上反序列化 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55040264/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com