mock简单的json返回

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xkhgnc_6666/article/details/51871085

针对非常简单的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;
	}
}
执行结果如下:



猜你喜欢

转载自blog.csdn.net/xkhgnc_6666/article/details/51871085