改章节个人在深圳游玩的时候突然想到的...这两天就有想写几篇关于参数利用的文章,所以回家到之后就奋笔疾书的写出来发表了

        SpringMVC构建REST接口的控制层,是全部接口的最顶层,也就是直接暴露给用户。

        在这层,对调用者身份以及对传入的参数进行验证,以及返回参数的组织。

        我爱好对象,不爱好以一个个的参数,再说我们用jackjson自动进行对象实例和Json的转换,所以我们需要输入输出参数的对象集。  由于目前SpringMVC对控制层的方法参数不支持泛型,所以我们需要在com.jiahe.rest.demo2.requestmodels建立相对应的请求参数对象;而输出可以使用泛型。

    一、输入参数组织

       顺次在com.jiahe.rest.demo2.requestmodels建立接口请求的头部对象和DishesCook的请求对象。

        1、接口请求的头部对象Header

    public class Header   implements Serializable  {

    private static final long serialVersionUID = 1L;

    
private String ck; //赐与调用者的,根据项目制订标准

    private String v;//调用登岸接口后的返回值,根据项目制订标准

    

利用Eclipse出产各个属性的get\set

    

利用Eclipse出产实例的字符串转换

    

利用Eclipse出产默许构造函数

    

利用Eclipse出产全字段的构造函数

    }

         2、DishesCook的请求对象

    public class DishesCookReauest   implements Serializable {

        private static final long serialVersionUID = 1L;

    
private Header header;//讨情头

    private DishesCook dishesCook;//请求对象

    

利用Eclipse出产各个属性的get\set

    

利用Eclipse出产实例的字符串转换

    

利用Eclipse出产默许构造函数

    

利用Eclipse出产全字段的构造函数

    }

    二、输出参数组织

        输出的Json,我们采取{"data":"","message":"",status:0}格式,因此需要在com.jiahe.rest.demo2.responseModels建立一个对应的泛型对象。

    public class ResultData<T> implements Serializable {

    
        private static final long serialVersionUID = 1L;

private int status;//返回 0:失败;1:胜利

private T data;//具体的对象

private String message;//错误时消息

    

利用Eclipse出产各个属性的get\set

    

利用Eclipse出产实例的字符串转换

    

利用Eclipse出产默许构造函数

    

利用Eclipse出产全字段的构造函数

    }

    三、DishesCook控制层实现

        1、在package com.jiahe.rest.demo2.controller下添加类DishesCookController

         2、其余和《SpringMVC构建REST接口:第四篇 第一个REST风格的接口》基本雷同,下面是代码



    每日一道理
美丽是平凡的,平凡得让你感觉不到她的存在;美丽是平淡的,平淡得只剩下温馨的回忆;美丽又是平静的,平静得只有你费尽心思才能激起她的涟漪。



@Controller
@RequestMapping("/dishescook")
public class DishesCookController {
	
	@Autowired
	private DishesCookService dishesCookService;
	
	/**
     * add  增加一个DishesCook
     * http://localhost:8080/demo2/dishescook/add
	 * @param requestData
	 * @param mode
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResultData<DishesCook> add(@RequestBody DishesCookRequest requestData,
			Model mode, HttpServletResponse response) throws IOException {		
		ResultData<DishesCook> resultData =new ResultData<DishesCook>();
		resultData.setStatus(0);
		resultData.setData(null);
		if (requestData==null) {			
			resultData.setMessage("参数错误:没有传入参数");
		} else {			
			//身份验证处理
			try {
				int i=dishesCookService.insert(requestData.getDishesCook());
				if (i==1){
					resultData.setStatus(1);
					resultData.setMessage("添加胜利");
				} else {
					resultData.setMessage("添加失败");
				}
			} catch (Exception e) {
				resultData.setMessage("添加失败:"+e.getMessage());
			}			
		}
		return resultData;
	}
	
	/**
	 * update  修改一个DishesCook
     * http://localhost:8080/demo2/dishescook/update
	 * @param requestData
	 * @param mode
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ResultData<DishesCook> update(@RequestBody DishesCookRequest requestData,
			Model mode, HttpServletResponse response) throws IOException {		
		ResultData<DishesCook> resultData =new ResultData<DishesCook>();
		resultData.setStatus(0);
		resultData.setData(null);
		if (requestData==null) {			
			resultData.setMessage("参数错误:没有传入参数");
		} else {			
			//身份验证处理
			try {
				int i=dishesCookService.update(requestData.getDishesCook());
				if (i==1){
					resultData.setStatus(1);
					resultData.setMessage("修改胜利");
				} else {
					resultData.setMessage("修改失败");
				}
			} catch (Exception e) {
				resultData.setMessage("修改失败:"+e.getMessage());
			}			
		}
		return resultData;
	}
	
	/**
     * deleteByID  删除一个DishesCook
     * http://localhost:8080/demo2/dishescook/deletebyid
	 * @param requestData
	 * @param mode
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/deletebyid", method = RequestMethod.POST)
	@ResponseBody
	public ResultData<DishesCook> deleteByID(@RequestBody DishesCookRequest requestData,
			Model mode, HttpServletResponse response) throws IOException {		
		ResultData<DishesCook> resultData =new ResultData<DishesCook>();
		resultData.setStatus(0);
		resultData.setData(null);
		if (requestData==null) {			
			resultData.setMessage("参数错误:没有传入参数");
		} else {			
			//身份验证处理
			try {
				int i=dishesCookService.deleteByID(requestData.getDishesCook());
				if (i==1){
					resultData.setStatus(1);
					resultData.setMessage("删除胜利");
				} else {
					resultData.setMessage("删除失败");
				}
			} catch (Exception e) {
				resultData.setMessage("删除失败:"+e.getMessage());
			}			
		}
		return resultData;
	}
	
	@RequestMapping(value = "/deletebycondition", method = RequestMethod.POST)
	@ResponseBody
	public ResultData<DishesCook> deleteByCondition(@RequestBody DishesCookRequest requestData,
			Model mode, HttpServletResponse response) throws IOException {		
		ResultData<DishesCook> resultData =new ResultData<DishesCook>();
		resultData.setStatus(0);
		resultData.setData(null);
		if (requestData==null) {			
			resultData.setMessage("参数错误:没有传入参数");
		} else {			
			//身份验证处理
			try {
				int i=dishesCookService.deleteByCondition(requestData.getDishesCook());
				if (i==1){
					resultData.setStatus(1);
					resultData.setMessage("删除胜利");
				} else {
					resultData.setMessage("删除失败");
				}
			} catch (Exception e) {
				resultData.setMessage("删除失败:"+e.getMessage());
			}			
		}
		return resultData;
	}
	
	@RequestMapping(value = "/findbyid", method = RequestMethod.POST)
	@ResponseBody
	public ResultData<DishesCook> findByID(@RequestBody DishesCookRequest requestData,
			Model mode, HttpServletResponse response) throws IOException {		
		ResultData<DishesCook> resultData =new ResultData<DishesCook>();
		resultData.setStatus(0);
		resultData.setData(null);
		if (requestData==null) {			
			resultData.setMessage("参数错误:没有传入参数");
		} else {			
			//身份验证处理
			try {
				DishesCook dishesCook =dishesCookService.findByID(requestData.getDishesCook());
				resultData.setStatus(1);
				resultData.setData(dishesCook);
			} catch (Exception e) {
				resultData.setMessage("查询失败:"+e.getMessage());
			}			
		}
		return resultData;
	}
	
	@RequestMapping(value = "/findbycondition", method = RequestMethod.POST)
	@ResponseBody
	public ResultData<List<DishesCook>> findByCondition(@RequestBody DishesCookRequest requestData,
			Model mode, HttpServletResponse response) throws IOException {		
		ResultData<List<DishesCook>> resultData =new ResultData<List<DishesCook>>();
		resultData.setStatus(0);
		resultData.setData(null);
		if (requestData==null) {			
			resultData.setMessage("参数错误:没有传入参数");
		} else {			
			//身份验证处理
			try {
				List<DishesCook> dishesCookList =dishesCookService.findByCondition(requestData.getDishesCook());
				resultData.setStatus(1);
				resultData.setData(dishesCookList);
			} catch (Exception e) {
				resultData.setMessage("查询失败:"+e.getMessage());
			}			
		}
		return resultData;
	}
	
	@RequestMapping(value = "/findcountbycondition", method = RequestMethod.POST)
	@ResponseBody
	public ResultData<Long> findCountByCondition(@RequestBody DishesCookRequest requestData,
			Model mode, HttpServletResponse response) throws IOException {		
		ResultData<Long> resultData =new ResultData<Long>();
		resultData.setStatus(0);
		resultData.setData(null);
		if (requestData==null) {			
			resultData.setMessage("参数错误:没有传入参数");
		} else {			
			//身份验证处理
			try {
				Long l =dishesCookService.findCountByCondition(requestData.getDishesCook());
				resultData.setStatus(1);
				resultData.setData(l);
			} catch (Exception e) {
				resultData.setMessage("查询失败:"+e.getMessage());
			}			
		}
		return resultData;
	}
}