jackson xml-无法反序列化布尔值

knpiaxh1  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(365)

我正试图用jackson解析xml内容,但在布尔值方面有一些困难。这是我的xml内容的一部分:

<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body xmlns:ns1="http://somecontent">
    <wsResponse xmlns="http://somecontent">
      <responseType>SUCCESS</responseType>
      <response>
        <successfullResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="InterchangeSearchResponse">
          <totalResult>1</totalResult>
          <returnedResults>1</returnedResults>
          <pageIndex>1</pageIndex>
          <interchanges>
            <interchange>
              <depositId>somecontent</depositId>
              <interchangeId>somecontent</interchangeId>
              <depositDate>2021-03-26T11:45:05.000+01:00</depositDate>
              <depositSubject>dépôt WS</depositSubject>
              <numADS>number</numADS>
              <adsDate>2021-03-26T11:45:05.000+01:00</adsDate>
              <alias>contentAsString</alias>
              <version xsi:nil="true"/>
              <isTest>false</isTest>
              <deposantAccount>
                <name>someString</name>
              </deposantAccount>
              <teleProcedure>someString</teleProcedure>
              <statesHistory>

这是我的xml类结构:

@JacksonXmlProperty(localName = "Body")
    private Body body;

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }
}
@JacksonXmlRootElement
public class Body {
    @JacksonXmlProperty(localName = "Fault")
    private Fault fault;
    private WsResponse wsResponse;

    public Fault getFault() {
        return fault;
    }
    public void setFault(Fault fault) {
        this.fault = fault;
    }
    public WsResponse getWsResponse() {
        return wsResponse;
    }
    public void setWsResponse(WsResponse wsResponse) {
        this.wsResponse = wsResponse;
    }
}
@JacksonXmlRootElement(localName = "wsResponse")
public class WsResponse {
    private String responseType;
    private Response response;

    public String getResponseType() {
        return responseType;
    }
    public void setResponseType(String responseType) {
        this.responseType = responseType;
    }

    public Response getResponse() {
        return response;
    }
    public void setResponse(Response response) {
        this.response = response;
    }
}
@JacksonXmlRootElement
public class Response {
    private SuccessfulResponse successfullResponse;
    private ErrorResponse errorResponse;

    public SuccessfulResponse getSuccessfullResponse() {
        return successfullResponse;
    }
    public void setSuccessfullResponse(SuccessfulResponse successfullResponse) {
        this.successfullResponse = successfullResponse;
    }

    public ErrorResponse getErrorResponse() {
        return errorResponse;
    }
    public void setErrorResponse(ErrorResponse errorResponse) {
        this.errorResponse = errorResponse;
    }
}
@JacksonXmlRootElement
public class SuccessfulResponse {
    /**
     * Response when add document success
     */
    private String depositId;

    /**
     * Response when get interchanges by deposit id success
     */
    private String type;
    private Integer totalResult;
    private Integer returnedResults;
    private Integer pageIndex;
    private Interchanges interchanges;

    /**
     * Response when get declaration details success
     */
    private DeclarationTdfc declarationTdfc;

    public SuccessfulResponse() {}

    public SuccessfulResponse(String depositId) {
        this.depositId = depositId;
    }

    public SuccessfulResponse(String type, Integer totalResult, Integer returnedResults, Integer pageIndex, Interchanges interchanges) {
        this.type = type;
        this.totalResult = totalResult;
        this.returnedResults = returnedResults;
        this.pageIndex = pageIndex;
        this.interchanges = interchanges;
    }

    public SuccessfulResponse(DeclarationTdfc declarationTdfc) {
        this.declarationTdfc = declarationTdfc;
    }

    public SuccessfulResponse(String depositId, String type, Integer totalResult, Integer returnedResults,
                              Integer pageIndex, Interchanges interchanges, DeclarationTdfc declarationTdfc) {
        super();
        this.depositId = depositId;
        this.type = type;
        this.totalResult = totalResult;
        this.returnedResults = returnedResults;
        this.pageIndex = pageIndex;
        this.interchanges = interchanges;
        this.declarationTdfc = declarationTdfc;
    }

    public String getDepositId() {
        return depositId;
    }
    public void setDepositId(String depositId) {
        this.depositId = depositId;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Integer getTotalResult() {
        return totalResult;
    }
    public void setTotalResult(Integer totalResult) {
        this.totalResult = totalResult;
    }
    public Integer getReturnedResults() {
        return returnedResults;
    }
    public void setReturnedResults(Integer returnedResults) {
        this.returnedResults = returnedResults;
    }
    public Integer getPageIndex() {
        return pageIndex;
    }
    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }
    public Interchanges getInterchanges() {
        return interchanges;
    }
    public void setInterchanges(Interchanges interchanges) {
        this.interchanges = interchanges;
    }

    public DeclarationTdfc getDeclarationTdfc() {
        return declarationTdfc;
    }

    public void setDeclarationTdfc(DeclarationTdfc declarationTdfc) {
        this.declarationTdfc = declarationTdfc;
    }
}
public class Interchanges {
    @JacksonXmlProperty(localName = "interchange")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Interchange> interchange;

    public Interchanges() { super(); }

    public Interchanges(List<Interchange> interchange) {
        super();
        this.interchange = interchange;
    }

    public List<Interchange> getInterchange() {
        return interchange;
    }

    public void setInterchange(List<Interchange> interchange) {
        this.interchange = interchange;
    }
}
public class Interchange {
    private String depositId;
    private Integer interchangeId;
    //@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = SelfmedConstants.Dates.ENGLISH_DATETIME_PATTERN)
    private String depositDate;
    private String depositSubject;
    private String numADS;
    //@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = SelfmedConstants.Dates.ENGLISH_DATETIME_PATTERN)
    private String adsDate;
    private String alias;
    //@JacksonXmlProperty(isAttribute = true)
    private String version;
    @JsonSerialize(using = BooleanSerializer.class)
    @JsonDeserialize(using = BooleanDeserializer.class)
    @JacksonXmlProperty(localName = "isTest")
    private Boolean isTest;
    @JacksonXmlProperty(localName = "name")
    @JacksonXmlElementWrapper(localName = "deposantAccount")
    private List<String> name;
    private String teleProcedure;
    private StatesHistory statesHistory;
    @JacksonXmlProperty(localName = "declarationId")
    @JacksonXmlElementWrapper(localName = "declarationIds")
    private List<String> declarationId;

    public Interchange() {
    }

    public Interchange(String depositId, Integer interchangeId, String depositDate, String depositSubject, String numADS,
                       String adsDate, String alias, String version, Boolean isTest, List<String> name, String teleProcedure,
                       StatesHistory statesHistory, List<String> declarationId) {
        this();
        this.depositId = depositId;
        this.interchangeId = interchangeId;
        this.depositDate = depositDate;
        this.depositSubject = depositSubject;
        this.numADS = numADS;
        this.adsDate = adsDate;
        this.alias = alias;
        this.version = version;
        this.isTest = isTest;
        this.name = name;
        this.teleProcedure = teleProcedure;
        this.statesHistory = statesHistory;
        this.declarationId = declarationId;
    }

    public String getDepositId() {
        return depositId;
    }
    public void setDepositId(String depositId) {
        this.depositId = depositId;
    }

    public Integer getInterchangeId() {
        return interchangeId;
    }

    public void setInterchangeId(Integer interchangeId) {
        this.interchangeId = interchangeId;
    }

    public String getDepositDate() {
        return depositDate;
    }

    public void setDepositDate(String depositDate) {
        this.depositDate = depositDate;
    }

    public String getDepositSubject() {
        return depositSubject;
    }

    public void setDepositSubject(String depositSubject) {
        this.depositSubject = depositSubject;
    }

    public String getNumADS() {
        return numADS;
    }

    public void setNumADS(String numADS) {
        this.numADS = numADS;
    }

    public String getAdsDate() {
        return adsDate;
    }

    public void setAdsDate(String adsDate) {
        this.adsDate = adsDate;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Boolean getIsTest() {
        return isTest;
    }

    public void setIsTest(Boolean isTest) {
        this.isTest = isTest;
    }

    public void setIsTest(String isTest) {
        this.isTest = Boolean.valueOf(isTest);
    }

    public List<String> getName() {
        return name;
    }

    public void setName(List<String> name) {
        this.name = name;
    }

    public String getTeleProcedure() {
        return teleProcedure;
    }

    public void setTeleProcedure(String teleProcedure) {
        this.teleProcedure = teleProcedure;
    }

    public StatesHistory getStatesHistory() {
        return statesHistory;
    }

    public void setStatesHistory(StatesHistory statesHistory) {
        this.statesHistory = statesHistory;
    }

    public List<String> getDeclarationId() {
        return declarationId;
    }

    public void setDeclarationId(List<String> declarationId) {
        this.declarationId = declarationId;
    }
}

正如你在交换课上看到的,我尝试了一些东西,但没有任何效果。
我生成的类是这样的:

JacksonXmlModule xmlModule = new JacksonXmlModule();
            xmlModule.setDefaultUseWrapper(false);

            XmlMapper xmlMapper = new XmlMapper(xmlModule);
            xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
            xmlMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES);
            System.out.println(responseAsString);
            Envelope envelope = xmlMapper.readValue(responseAsString, new TypeReference<>() {

但是当我试图解析我的内容时,我得到了一个例外:
无法构造的示例 payload.response.Interchange (尽管至少存在一个创建者):没有字符串参数构造函数/工厂方法从[source:(stringreader)处的字符串值('false')反序列化;行:20,列:28](通过引用链:payload.response.envelope[“body”]->payload.response.body[“wsresponse”]->payload.response.wsresponse[“response”]->payload.response.response[“successfullresponse”]->payload.response.interchanges[“interchange”]->java.util.arraylist[1])
我尝试了很多东西,但都不管用,所以我想知道问题是否不在这里。。。如果您有任何解决方案或线索探索,请让我知道!
谢谢。

v09wglhw

v09wglhw1#

根据javabean规范,您必须命名getter isTest() .

相关问题