• 使用FireFox插件RESTClient、HttpRequester模拟http(get post)请求


    我们写好一个接口后,需要进行测试。有时我们会写一个html表单提交,无疑增加了工作量,尤其是当参数比较多或者传json或xml数据时,效率更是大大降低。我们可以使用基于FireFox的RESTClient或HttpRequester插件来进行测试。

    打开火狐浏览器,在右上角的菜单中有附加组件,进入其中,直接搜索这两个插件,安装后重启即可,安装完后在浏览器右上方生成了插

    件的图标。

    1 RESTClient

    发送get请求时参数在url中;当发送post请求传参时需要设置头信息,点击上图中的Headers-Custom Header进行设置

    2 HttpRequester

    RestClient

    1.传输单个参数

    content-type:application/json;charset=UTF-8

    requestbody

    {
    "username":"qqq"
    }

    Controller

            @RequestMapping(value = "/restClinet")
            @ResponseBody
            public String restClinet(@RequestBody String username) {
    
                logger.info("TestRestClient" + username  );
                // save trade
                
                return username;
            }

    2.传输多个参数(对象)

    {
    "tradeId":"66",
    "tradeServer":"一梦十年",
    "tradeCurrency":"4500",
    "tradeMoney":"100",
    "tradeTime":"2018年10月10日 12:24:16",
    "tradeComment":"333"
    }
    @RequestMapping(value = "/restClinet")
            @ResponseBody
            public Trade restClinet(@RequestBody Trade trade) {
    
                logger.info("TestRestClient"  + trade);
                // save trade
                
                return trade;
            }

    复合型json格式

    {
    "stuId":"66",
    "stuName":"小红",
    "stuSex":"girl",
    "book":{
              "bookName":"math",
              "bookOwner":"Jim"
             }
    
    }

    ErrorCode

    package com.my.base;
    
    public enum Error{    
        
        S001("S001","Success"),
        E001("E001","System error!"),
        E002("E002","Sorroy,user cannot find in database!"),
        E003("E003","Unknow error!");
        
        private String errorCode;
        private String errorMessage;
        public String getErrorCode() {
            return errorCode;
        }
        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }
        public String getErrorMessage() {
            return errorMessage;
        }
        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }
        private Error(String errorCode, String errorMessage) {
            this.errorCode = errorCode;
            this.errorMessage = errorMessage;
        }  
        
        
    }
    @RequestMapping(value = "/restClinet")
            @ResponseBody
            public Map<String, Object> restClinet(@RequestBody Trade trade) {
    
                logger.info("TestRestClient"  + trade);
                // save trade
                Map<String, Object> results = new HashMap<String, Object>();
                results.put(Error.S001.getErrorCode(),Error.S001.getErrorMessage());
                results.put("trades",this.tradeService.getAllTrades());            
                
                return results;
            }

    返回模版信息

     3.HttpHeaders传输数据

    1)添加HttpHeaders

    2)获取HttpHeaders

    @RequestMapping(value = "/restClinet")
            @ResponseBody
            public Map<String, Object> restClinet(@RequestBody Trade trade,@RequestHeader HttpHeaders headers) {
    
                logger.info("TestRestClient"  + trade);
                // save trade
                String userCountry = headers.getFirst("userType");
                Map<String, Object> results = new HashMap<String, Object>();
                results.put(Error.S001.getErrorCode(),Error.S001.getErrorMessage());
                results.put("userCountry", userCountry);
                results.put("trades",this.tradeService.getAllTrades());    
                
                
                return results;
            }

     或者

            @RequestMapping(value = "/restClinet")
            @ResponseBody
            public Map<String, Object> restClinet(@RequestBody Trade trade,@RequestHeader("userType") String  userType) {
    
                logger.info("TestRestClient"  + trade);
                // save trade
                Map<String, Object> results = new HashMap<String, Object>();
                results.put(Error.S001.getErrorCode(),Error.S001.getErrorMessage());
                results.put("userType", userType);
                results.put("trades",this.tradeService.getAllTrades());    
                
                
                return results;
            }
  • 相关阅读:
    python 文件读写操作(转抄)
    kubernetes之kubeadmin安装部署
    bash之字符串处理(核心重点)
    blocking and nonblocking
    文件格式转换
    解压.asar
    Cocos Creator Editor 编辑器扩展记录
    CocosCreator 警告:Please set node's active instead of rigidbody's enabled
    Unity 垂直翻转位图颜色数据
    CocosCreator 动态设置属性在Properties面板显示/隐藏
  • 原文地址:https://www.cnblogs.com/lukelook/p/9764490.html
Copyright © 2020-2023  润新知