針對非常簡單的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; } }