簡體   English   中英

RestTemplate XML對具有屬性的空標記反序列化null

[英]RestTemplate XML deserialize null on empty tag with attributes

我有以下的回應,當值在屬性和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注釋。 可能還需要將JaxbAnnotationModule注冊到Jacksons XmlMapper:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM