zoukankan      html  css  js  c++  java
  • 两种接口传送数据协议(xml和json)

       规范性接口开发中,一般数据是以json或者xml的格式传送的,而不是字符串的形式直接返回给接口调用者;下面介绍这两种格式的编写方法。

       下面两种数据传送方式是接口输出端的编写。需引入的包如下:

    <span style="font-family:KaiTi_GB2312;font-size:18px;">importjava.io.PrintWriter;
    
    importjava.io.StringWriter;
    
    importjavax.servlet.http.HttpServletResponse;
    
    importorg.dom4j.Document;
    
    import org.dom4j.io.OutputFormat;
    
    import org.dom4j.io.XMLWriter;</span>


    1.传送 XML 协议的格式数据

    <span style="font-family:KaiTi_GB2312;font-size:18px;">   /**
    
        * xml 格式数据
    
        *
    
        * @param response
    
        * @param doc
    
        */
    
        publicstaticvoidoutDocToXML(HttpServletResponse response, Document doc) {
    
           // 以下代码请注意编码顺序
    
           response.setCharacterEncoding("utf-8");
    
           response.setContentType("text/xml;charset=utf-8");
    
           response.setHeader("Pragma", "no-cache");
    
           response.setHeader("Cache-Control", "no-cache");
    
           response.setDateHeader("Expires", 0);
    
           PrintWriter out = null;
    
           StringWriter writer = new StringWriter();
    
           OutputFormat format = OutputFormat.createPrettyPrint(); //
    
           format.setEncoding("UTF-8");
    
           XMLWriter xmlwriter = new XMLWriter(writer,format);
    
     
    
           try {
    
                out = response.getWriter();
    
               xmlwriter.write(doc);
    
           } catch (Exception e) {
    
               e.printStackTrace();
    
           }
    
           out.print(writer.toString());
    
           out.close();
    
    }</span>


     

    2.传送 json 协议的格式数据

    <span style="font-family:KaiTi_GB2312;font-size:18px;">/**
    
        * json 数据格式
    
        *
    
        * @param response
    
        * @param data
    
        * @param type
    
        */
    
        protectedvoidsendData(HttpServletResponse response, Object data) {
    
           if (data != null) {
    
               response.setCharacterEncoding("utf-8");
    
               response.setContentType("application/json;charset=UTF-8");
    
               response.setHeader("Pragma", "no-cache");
    
               response.setHeader("Cache-Control", "no-cache");
    
               response.setDateHeader("Expires", 0);
    
               PrintWriter out = null;
    
               try {
    
                    out = response.getWriter();
    
                    out.print(data);
    
                    out.close();
    
               } catch (Exception e) {
    
                    e.printStackTrace();
    
               }
    
           }
    
        }</span>

    转正请指明出处:blog.csdn.net/yangkai_hudong
  • 相关阅读:
    Python heapq 模块的实现
    使用Python在2M内存中排序一百万个32位整数
    heapq
    将不确定变成确定~Uri文本文件不用浏览器自动打开,而是下载到本地
    说说设计模式~组合模式(Composite)
    JS~字符串长度判断,超出进行自动截取(支持中文)
    DDD~基础设施层~续
    谈谈设计模式~原型模式(Prototype)
    Study note for Continuous Probability Distributions
    Spring——AOP配置时的jar包异常
  • 原文地址:https://www.cnblogs.com/yangkai-cn/p/4016559.html
Copyright © 2011-2022 走看看