【问题标题】:WebResponse posting a null stringWebResponse 发布一个空字符串
【发布时间】:2016-07-18 15:41:48
【问题描述】:

我正在尝试根据下面的 curl 命令从 html 文本区域发布 xml

curl -X POST --data-urlencode 'xml=<hml>...</hml>' http://miring.b12x.org/validator/ValidateMiring/

这是我目前的代码

@Produces("application/xml")
public String validate(@FormParam("xml") String xml) {

    try {
        Client client = Client.create();

        WebResource webResource = client.resource("http://miring.b12x.org/validator/ValidateMiring/");

                                                  // POST method
        //Posting null for some reason.
        ClientResponse response = webResource.accept("application/xml").post(ClientResponse.class, xml);
        // check response status code
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }

        // display response
        String output = response.getEntity(String.class);
        System.out.println("Output from Server .... ");
        System.out.println(output + "\n");
        return output;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "Oops";
}

当我使用经过测试可用于 web 应用程序的 XML 运行此程序时,我不断收到空条目响应。

我不知道从哪里开始

编辑: 主要问题已在下面回答。下面的 xml 中带有 + 的错误是一个示例。在“共识序列”块下是带有链的“+”。所有其他枚举都有效(-,-1,1)

    <?xml version="1.0" encoding="UTF-8"?>

<hml xmlns="http://schemas.nmdp.org/spec/hml/1.0.1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://schemas.nmdp.org/spec/hml/1.0.1  http://schemas.nmdp.org/spec/hml/1.0.1/hml-1.0.1.xsd"
 version="1.0.1" >

 <!-- 
  MIRING Element 1.1 requires the inclusion of an hmlid.
  hmlid can be reported in the form of an ISO Object Identifier (OID)
  "root" represents a unique publically registered organization
  "extension" is a unique document id managed by the reporting organization.
 -->

 <hmlid root="2.34.48.32" extension="HML.3245662"/>

 <!-- 
  MIRING Element 1.2 requires the inclusion of a reporting-center.
  reporting-center identifies the organization sending the HML message.
  "reporting-center-id" is a unique identifier of the sender.
  "reporting-center-context" reports the context/naming authority of the identifier.
 -->

 <reporting-center reporting-center-id="567"/>
 <sample id="4555-6677-8">
  <typing gene-family="HLA" date="2015-01-13">

   <!-- 
    MIRING Element 3 requires the inclusion of Genotyping information.
    The Genotype should include all pertinent Loci, as well as a Genotype in a standard format.  
    GLStrings can be included either as plain text, or as a reference to a publicly
    available service, such as GL Service (gl.nmdp.org)
   -->

   <allele-assignment date="2015-07-28" allele-db="IMGT/HLA" allele-version="3.17.0">
    <haploid locus="HLA-A" method="DNA" type="02:20:01"/>
    <glstring>
     HLA-A*02:20:01
    </glstring>
   </allele-assignment>
   <typing-method>

   <!-- 
    MIRING Element 6 requires platform documentation.  This could be a peer-reviewed publication,
    or an identifier of a procedure on a publicly available resource, such as NCBI GTR
   -->

    <sbt-ngs locus="HLA-A"
     test-id="HLA-A.Test.1234"
     test-id-source="AcmeGenLabs">
     <raw-reads uri="rawreads/read1.fastq.gz"
      availability="public"
      format="fastq"
      paired="1"
      pooled="1"
      adapter-trimmed="1"
      quality-trimmed="0"/>
    </sbt-ngs>
   </typing-method>
   <consensus-sequence date="2015-01-13">

    <!-- 
     MIRING Element 2 requires the inclusion of Reference Context.
     The location and identifiers of the reference sequence should be specified. 
     start and end attributes are 0-based, and refer to positions on the reference sequence.
    --> 

    <reference-database availability="public" curated="true">
     <reference-sequence
      name="HLA-A reference"
      id="Ref111"
      start="945000"
      end="946000"
      accession="GL000123.4"
      uri="http://AcmeGenReference/RefDB/GL000123.4"/>
    </reference-database>

    <!-- 
     MIRING Element 4 requires the inclusion of a consensus sequence.
     The start and end positions are 0-based, and refer to positions on the reference sequence (reference-sequence-id)
     Multiple consensus-sequence-block elements can be included sequentially.
    -->

    <consensus-sequence-block reference-sequence-id="Ref111"
     start="945532"
     end="945832"
     strand="+"
     phase-set="1"
     expected-copy-number="1"
     continuity="true"
     description="HLA-A Consensus Sequence 4.5.67">

     <!-- 
      A sequence can be reported as plain text, or as a pointer to an external reference,
      or as variants from a reference sequence.
     -->

     <sequence>
      CCCAGTTCTCACTCCCATTGGGTGTCGGGTTTCCAGAGAAGCCAATCAGTGTCGTCGCGGTCGCTGTTCTAAAGCCCGCACGCACCCACCGGGACTCAGATTCTCCCCAGACGCCGAGGATGGCCGTCATGGCGCCCCGAACCCTCCTCCTGCTACTCTCGGGGGCCCTGGCCCTGACCCAGACCTGGGCGGGTGAGTGCGGGGTCGGGAGGGAAACCGCCTCTGCGGGGAGAAGCAAGGGGCCCTCCTGGCGGGGGCGCAGGACCGGGGGAGCCGCGCCGGGACGAGGGTCGGGCAGGT
     </sequence>

     <!-- 
      MIRING Element 5 requires the inclusion of any relevant sequence polymorphisms.  
      These represent variants from the reference sequence.
      start and end attributes are 0-based, and refer to positions on the reference sequence.
      You can see this variant at positions 10 - 15 on the sequence. (945542 - 945532 = 10)
     -->

     <variant id="0"
      reference-bases="GTCATG"
      alternate-bases="ACTCCC"
      start="945542"
      end="945548"
      filter="pass"
      quality-score="95">

      <!-- 
       The functional effects of variants can be reported using variant-effect.  
       They should use Sequence Ontology (SO) variant effect terms.
      -->

      <variant-effect term="missense_variant"/>
     </variant>
    </consensus-sequence-block>
   </consensus-sequence>
  </typing>
 </sample>

 <!-- 
  Multiple samples can be included in a single message.  
  Each sample should have it's own reference-database(s) even if they are identical to other samples' references. 
 -->

 <sample id="4555-6677-9">
  <typing gene-family="HLA" date="2015-01-13">
   <allele-assignment date="2015-07-28" allele-db="IMGT/HLA" allele-version="3.17.0">
    <haploid locus="HLA-A" method="DNA" type="02:20:01"/>
    <glstring>
     HLA-A*02:01:01:01
    </glstring>
   </allele-assignment>
   <typing-method>
    <sbt-ngs locus="HLA-A"
     test-id="HLA-A.Test.1234"
     test-id-source="AcmeGenLabs">
     <raw-reads uri="rawreads/read2.fastq.gz"
      availability="public"
      format="fastq"
      paired="1"
      pooled="1"
      adapter-trimmed="1"
      quality-trimmed="0"/>
    </sbt-ngs>
   </typing-method>
   <consensus-sequence date="2015-01-13">
    <reference-database availability="public" curated="true">
     <reference-sequence
      name="HLA-A reference"
      id="Ref112"
      start="945000"
      end="946000"
      accession="GL000123.4"
      uri="http://AcmeGenReference/RefDB/GL000123.4"/>
    </reference-database>
    <consensus-sequence-block 
     reference-sequence-id="Ref112"
     start="945532"
     end="945832"
     strand="+"
     phase-set="1"
     expected-copy-number="1"
     continuity="true"
     description="HLA-A Consensus Sequence 4.5.89">
     <sequence>
      CCCAGTTCTCGTCATGATTGGGTGTCGGGTTTCCAGAGAAGCCAATCAGTGTCGTCGCGGTCGCTGTTCTAAAGCCCGCACGCACCCACCGGGACTCAGATTCTCCCCAGACGCCGAGGATGGCCGTCATGGCGCCCCGAACCCTCCTCCTGCTACTCTCGGGGGCCCTGGCCCTGACCCAGACCTGGGCGGGTGAGTGCGGGGTCGGGAGGGAAACCGCCTCTGCGGGGAGAAGCAAGGGGCCCTCCTGGCGGGGGCGCAGGACCGGGGGAGCCGCGCCGGGACGAGGGTCGGGCAGGT
     </sequence>
    </consensus-sequence-block>
   </consensus-sequence>
  </typing>
 </sample>

</hml>

谢谢

【问题讨论】:

  • 尝试删除 XML= --data-urlencode '...'..something like this.

标签: java xml rest post httpwebresponse


【解决方案1】:

来自@FormParam 的Javadoc(强调我的):

将请求实体正文中包含的表单参数的绑定到资源方法参数。

也就是说,如果您调用 validate() 方法,例如像这样来自 cURL

curl -X POST --data-urlencode 'xml=<hml>...</hml>' http://localhost:8080/validate

然后方法参数xml 将绑定到值&lt;hml&gt;...&lt;/hml&gt;ValidateMiring 服务在该值上返回消息XML is null.(至少对我而言)。

如果您在将方法参数发布到实际的 Web 服务之前,在方法参数前面加上前缀 xml=,您应该会得到不同的结果:

ClientResponse response = webResource.accept("application/xml").post(ClientResponse.class, "xml=" + xml);

【讨论】:

  • 这太棒了!不过,问题是,虽然它接受 xml,但它是否有理由取代在我的 xml 中不接受“+”,尽管它是允许的枚举的一部分?说值 ', ' 不被接受,虽然它是一个“+”
  • 我刚刚检查了其他枚举,只有 + 不起作用(其他枚举是 -、-1 和 1)
  • @Georgrio:您能否提供一个包含此 Web 服务不接受的“+”字符的 XML 字符串示例?
  • @Georgrio:我怀疑他们的 Web 服务的实现存在错误。例如,当我将您的 XML 文件上传到他们的 Web UI 时,它会得到验证,没有任何问题。此外,由据称错误的strandattribute 引起的错误消息看起来好像加号被丢弃在您的客户端和他们的 XML 验证器之间的某个地方。他们似乎已经标记了错误消息,这就是逗号的来源:元素'consensus-sequence-block'上的属性'strand'的值''相对于它'是无效的......
猜你喜欢
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多