代码之家  ›  专栏  ›  技术社区  ›  Ozgur Altuntas

由于使用Java中的JAXB库从XML代码中获取空结果,因此名称空间有什么问题?

  •  0
  • Ozgur Altuntas  · 技术社区  · 2 年前

    下面的代码运行良好,我从这段代码中得到的结果如下图所示

    my result

    @XmlRootElement(name = "AssumeRoleResponse")
    public class AssumeRoleResponse {
      protected AssumeRoleResult assumeRoleResult;
    
      protected ResponseMetadata responseMetadata;
    
      public AssumeRoleResult getAssumeRoleResult() {
        return assumeRoleResult;
      }
    
      @XmlElement(name = "AssumeRoleResult")
      public void setAssumeRoleResult(AssumeRoleResult assumeRoleResult) {
        this.assumeRoleResult = assumeRoleResult;
      }
    
      public ResponseMetadata getResponseMetadata() {
        return responseMetadata;
      }
    
      @XmlElement(name = "ResponseMetadata")
      public void setResponseMetadata(ResponseMetadata responseMetadata) {
        this.responseMetadata = responseMetadata;
      }
    }
    

    我在上面的代码中使用了下面的XML字符串,效果很好,但是,

    <AssumeRoleResponse>
      <AssumeRoleResult>
        <AssumedRoleUser>
          <AssumedRoleId>AR..............:Test</AssumedRoleId>
          <Arn>arn:aws:sts::7..........:assumed-role/amazon_spi_role/Test</Arn>
        </AssumedRoleUser>
        <Credentials>
          <AccessKeyId>A.............</AccessKeyId>
          <SecretAccessKey>my.......................</SecretAccessKey>
          <SessionToken>................................</SessionToken>
          <Expiration>2022-06-27T18:55:34Z</Expiration>
        </Credentials>
      </AssumeRoleResult>
      <ResponseMetadata>
        <RequestId>7241c78f-a975-4486-b4e0-6bf6d9b5c37b</RequestId>
      </ResponseMetadata>
    </AssumeRoleResponse>
    

    但是,当我在XmlRootElement中使用名称空间时,我无法从代码中得到结果,我尝试了几种方法,但没有从中得到任何结果

    代码如下

    @XmlRootElement(namespace = "https://sts.amazonaws.com/doc/2011-06-15/", name = "AssumeRoleResponse")
    public class AssumeRoleResponse {
      protected AssumeRoleResult assumeRoleResult;
    
      protected ResponseMetadata responseMetadata;
    
      public AssumeRoleResult getAssumeRoleResult() {
        return assumeRoleResult;
      }
    
      @XmlElement(name = "AssumeRoleResult")
      public void setAssumeRoleResult(AssumeRoleResult assumeRoleResult) {
        this.assumeRoleResult = assumeRoleResult;
      }
    
      public ResponseMetadata getResponseMetadata() {
        return responseMetadata;
      }
    
      @XmlElement(name = "ResponseMetadata")
      public void setResponseMetadata(ResponseMetadata responseMetadata) {
        this.responseMetadata = responseMetadata;
      }
    }
    

    XML字符串在下面

    <AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
      <AssumeRoleResult>
        <AssumedRoleUser>
          <AssumedRoleId>AR..............:Test</AssumedRoleId>
          <Arn>arn:aws:sts::7..........:assumed-role/amazon_spi_role/Test</Arn>
        </AssumedRoleUser>
        <Credentials>
          <AccessKeyId>A.............</AccessKeyId>
          <SecretAccessKey>my.......................</SecretAccessKey>
          <SessionToken>................................</SessionToken>
          <Expiration>2022-06-27T18:55:34Z</Expiration>
        </Credentials>
      </AssumeRoleResult>
      <ResponseMetadata>
        <RequestId>7241c78f-a975-4486-b4e0-6bf6d9b5c37b</RequestId>
      </ResponseMetadata>
    </AssumeRoleResponse>
    

    结果如下 null result

    命名空间有什么问题,请告知

    0 回复  |  直到 2 年前