【问题标题】:set xml response header in spring webservice response在spring webservice响应中设置xml响应头
【发布时间】:2020-05-15 10:08:27
【问题描述】:

我想在 spring webservice 响应中返回这种类型的标头,谁能帮忙

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <PullURIResponse xmlns:ns2="http://tempuri.org/"> <ResponseStatus Status="1" ts="2016-01-11T14:44:48+05:30" txn="123456789">1</ResponseStatus>//1-Success //0-Failure //9-Pending <DocDetails> <DocType>INCER</DocType>
</DocDetails></PullURIResponse>

【问题讨论】:

    标签: java xml spring spring-boot web-services


    【解决方案1】:

    // 一个但是很麻烦但是没有任何额外的库

    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    public class XMLTextExtractor {
        static String  val;
    
        public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(new ByteArrayInputStream(val.getBytes()));
            XPath xpath = XPathFactory.newInstance().newXPath();
    
            XPathExpression expr = xpath.compile("//text()");
            NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            String textResult = "";
    
            for (int i = 0; i < nodes.getLength(); i++) {
                textResult += nodes.item(i).getNodeValue().trim().replaceAll("\\R+","");
            }
    
            System.out.println(textResult);
        }
    
        static {
            val = "<?xml version=\"1.0\"?><NGOSearchDocumentExt_Input> \n" +
                    "    <Option>NGOSearchDocumentExt</Option><CabinetName>INOMNUTDB</CabinetName> \n" +
                    "    <UserDBId>468315129</UserDBId><SearchText></SearchText><Rank>Y</Rank> \n" +
                    "    <SearchOnPreviousVersions>N</SearchOnPreviousVersions> \n" +
                    "    <LookInFolder>0</LookInFolder><IncludeSubFolder>Y</IncludeSubFolder> \n" +
                    "    <Name></Name><Owner></Owner><CreationDateRange></CreationDateRange> \n" +
                    "    <ExpiryDateRange></ExpiryDateRange><AccessedDateRange> \n" +
                    "    </AccessedDateRange><RevisedDateRange></RevisedDateRange> \n" +
                    "    <DataDefCriterion><DataDefCriteria><DataDefIndex>13</DataDefIndex> \n" +
                    "    <IndexId>103</IndexId><Operator></Operator><IndexValue>12</IndexValue> \n" +
                    "    <JoinCondition>AND</JoinCondition></DataDefCriteria>\n" +
                    "    <DataDefCriteria><DataDefIndex>13</DataDefIndex><IndexId>103</IndexId> \n" +
                    "    <Operator>like</Operator><IndexValue>abc</IndexValue> \n" +
                    "    <JoinCondition>AND</JoinCondition></DataDefCriteria></DataDefCriterion> \n" +
                    "    <SearchScope>0</SearchScope><PreviousList></PreviousList> \n" +
                    "    <SearchOnAlias>N</SearchOnAlias><Keywords></Keywords> \n" +
                    "    <GlobalIndexCriterion></GlobalIndexCriterion> \n" +
                    "    <ReferenceFlag>O</ReferenceFlag><SortOrder>D</SortOrder> \n" +
                    "    <GroupIndex>0</GroupIndex><StartFrom>1</StartFrom> \n" +
                    "    <NoOfRecordsToFetch>10</NoOfRecordsToFetch><OrderBy>5</OrderBy><CheckOutStatus></CheckOutStatus><MaximumHitCountFlag>Y</MaximumHitCountFlag><CheckOutByUser></CheckOutByUser><ObjectTypes><ObjectType>1</ObjectType><ObjectType>2</ObjectType><ObjectType>8</ObjectType><ObjectType>11</ObjectType><ObjectType>13</ObjectType><ObjectType>14</ObjectType><ObjectType>15</ObjectType><ObjectType>16</ObjectType><ObjectType>17</ObjectType><ObjectType>20</ObjectType></ObjectTypes><DataAlsoFlag>Y</DataAlsoFlag><CreatedByAppName></CreatedByAppName><Author></Author><AnnotationFlag>Y</AnnotationFlag><LinkDocFlag>Y</LinkDocFlag><PrevDocIndex>0</PrevDocIndex><IncludeSystemFolder>NN</IncludeSystemFolder><IncludeTrashFlag>N</IncludeTrashFlag><ThesaurusFlag>N</ThesaurusFlag><RecordAlsoFlag>N</RecordAlsoFlag></NGOSearchDocumentExt_Input>";
        }
    }
    

    【讨论】:

      【解决方案2】:

      与 Kaira 的回应类似,但侧重于 IndexValue 元素:

      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      InputSource inputSource = new InputSource(new StringReader(xmlString));
      Document document = documentBuilder.parse(inputSource);
      XPathFactory xPathFactory = XPathFactory.newInstance();
      XPath xpath = xPathFactory.newXPath();
      XPathExpression expression = xpath.compile("//IndexValue/text()");
      NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
      
      for (int n = 0; n < nodeList.getLength(); n++) {
          System.out.println(n + ": " + nodeList.item(n).getNodeValue());
      }
      

      【讨论】:

        【解决方案3】:

        如果你痴迷于简短的编码,那么它可能更合适(但不是我的风格:-))

        public class XMLTextExtractor {
            static String  val;
        
            public static void main(String[] args) {
        
                String textResult = "";
                for (String fragment: val.replace("<?xml version=\"1.0\"?>", "").split(">")){
                    textResult += fragment.replaceAll("<\\/.*|<.*","").trim();
                }
                System.out.println(textResult);
            }
        
            static {
                val = "<?xml version=\"1.0\"?><NGOSearchDocumentExt_Input> \n" +
                        "    <Option>NGOSearchDocumentExt</Option><CabinetName>INOMNUTDB</CabinetName> \n" +
                        "    <UserDBId>468315129</UserDBId><SearchText></SearchText><Rank>Y</Rank> \n" +
                        "    <SearchOnPreviousVersions>N</SearchOnPreviousVersions> \n" +
                        "    <LookInFolder>0</LookInFolder><IncludeSubFolder>Y</IncludeSubFolder> \n" +
                        "    <Name></Name><Owner></Owner><CreationDateRange></CreationDateRange> \n" +
                        "    <ExpiryDateRange></ExpiryDateRange><AccessedDateRange> \n" +
                        "    </AccessedDateRange><RevisedDateRange></RevisedDateRange> \n" +
                        "    <DataDefCriterion><DataDefCriteria><DataDefIndex>13</DataDefIndex> \n" +
                        "    <IndexId>103</IndexId><Operator></Operator><IndexValue>12</IndexValue> \n" +
                        "    <JoinCondition>AND</JoinCondition></DataDefCriteria>\n" +
                        "    <DataDefCriteria><DataDefIndex>13</DataDefIndex><IndexId>103</IndexId> \n" +
                        "    <Operator>like</Operator><IndexValue>abc</IndexValue> \n" +
                        "    <JoinCondition>AND</JoinCondition></DataDefCriteria></DataDefCriterion> \n" +
                        "    <SearchScope>0</SearchScope><PreviousList></PreviousList> \n" +
                        "    <SearchOnAlias>N</SearchOnAlias><Keywords></Keywords> \n" +
                        "    <GlobalIndexCriterion></GlobalIndexCriterion> \n" +
                        "    <ReferenceFlag>O</ReferenceFlag><SortOrder>D</SortOrder> \n" +
                        "    <GroupIndex>0</GroupIndex><StartFrom>1</StartFrom> \n" +
                        "    <NoOfRecordsToFetch>10</NoOfRecordsToFetch><OrderBy>5</OrderBy><CheckOutStatus></CheckOutStatus><MaximumHitCountFlag>Y</MaximumHitCountFlag><CheckOutByUser></CheckOutByUser><ObjectTypes><ObjectType>1</ObjectType><ObjectType>2</ObjectType><ObjectType>8</ObjectType><ObjectType>11</ObjectType><ObjectType>13</ObjectType><ObjectType>14</ObjectType><ObjectType>15</ObjectType><ObjectType>16</ObjectType><ObjectType>17</ObjectType><ObjectType>20</ObjectType></ObjectTypes><DataAlsoFlag>Y</DataAlsoFlag><CreatedByAppName></CreatedByAppName><Author></Author><AnnotationFlag>Y</AnnotationFlag><LinkDocFlag>Y</LinkDocFlag><PrevDocIndex>0</PrevDocIndex><IncludeSystemFolder>NN</IncludeSystemFolder><IncludeTrashFlag>N</IncludeTrashFlag><ThesaurusFlag>N</ThesaurusFlag><RecordAlsoFlag>N</RecordAlsoFlag></NGOSearchDocumentExt_Input>";
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2018-09-01
          • 2013-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多