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;
	}
}
执行结果如下:



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值