RestTemplate XML使用属性在空标记上反序列化null

问题描述 投票:0回答:1

我有以下响应,当值在属性中时,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;
java spring deserialization resttemplate objectmapper
1个回答
1
投票

首先,你应该在你的收藏类型上使用@JacksonXmlElementWrapper(useWrapping = false)。接下来尽量不要将JAXB Annotations与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);
© www.soinside.com 2019 - 2024. All rights reserved.