zoukankan      html  css  js  c++  java
  • mock简单的json返回

    针对非常简单的json返回串,我们也不一定非得通过freemarker模板的方式来构造返回数据,这里看实际的需求,如果返回的内容是固定的,而且json又非常简单,我们也可以直接写在程序里面,下面的接口采用post提交的方式,提交的是一个json串,返回的也是一个简单的json串;

    请求方式

    POST /quote

    发送包体的示例:
    发送包体示例
    {
        "vehicle_id": "287ncekk7lj3qkpw",
        "city_code": 110100,
        "selection": {
            "damage": 1,
            "pilfer": 1,
        },
        "biz_start_date": "2015-08-26",
        "force_start_date": "2015-08-26",
    }
    返回值:
    {
        "success": true,
        "code": 200,
        "data": {
            "request_id": "l1g9i2vmwowk36pg",           // 报价请求流水号
        }        
    }
    由于调用接口返回的内容是固定的,所以这里我们也不关心传来的包体里面的具体数据是咋样的,只需要其中的值非空即可,通过@RequestBody来接收一个Json对象的字符串;
    package com.mockCommon.controller.mock.youbi;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.mockCommon.constant.LogConstant;
    
    @Controller("createBaoJiaMockController")
    public class CreateBaoJiaMockController {
        
        @RequestMapping(value="/quote",method=RequestMethod.POST)  
        @ResponseBody
        public String createBaojia(@RequestBody Map<String, Object> params) {
            LogConstant.runLog.info("[YoubiJiekouCreateBaojia]parameter vehicle_id:" + params.get("vehicle_id") + ", city_code:" + params.get("city_code")+"" + ",selection:" + params.get("selection"));
             if(params.get("vehicle_id")==null || params.get("city_code")==null || params.get("selection")==null){
                 return "传递参数不正确";
             }
             String result;
             result = "{"success": true,"code": 200,"data": {"request_id": "l1g9i2vmwowk36pg"}}";
            return result;
        }
    }
  • 相关阅读:
    VS2013 快捷键乱掉如何修改回来
    WCF契约之---服务契约 、数据契约、 消息契约
    成功的背后!(给所有IT人)----转载:来自CSDN第一名博主
    C# Attribute(特性)之---契约---[ServiceContract] 、 [OperationContract]
    SQL Server数据库连接字符串整理
    大写String和小写string的区别
    C#经典之Application.DoEvents()的使用
    C#实现多态之一抽象
    C# Attribute(特性)之---数据契约 [DataContract]
    Druid数据库连接池获取连接阻塞(转载)
  • 原文地址:https://www.cnblogs.com/zyh0430/p/11921386.html
Copyright © 2011-2022 走看看