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;//调用登陆接口后的返回值,根据项目制定标准
...ToAdd get/set 利用Eclipse生产各个属性的get\set
...ToAdd toString() 利用Eclipse生产实例的字符串转换
...ToAdd Constructors from super 利用Eclipse生产默认构造函数
...ToAdd Constructor use fields 利用Eclipse生产全字段的构造函数
}2、DishesCook的请求对象
public class DishesCookReauest implements Serializable {
private static final long serialVersionUID = 1L;
private Header header;//求情头
...ToAdd get/set 利用Eclipse生产各个属性的get\set
...ToAdd toString() 利用Eclipse生产实例的字符串转换
...ToAdd Constructors from super 利用Eclipse生产默认构造函数
...ToAdd Constructor use fields 利用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;//错误时消息
...ToAdd get/set 利用Eclipse生产各个属性的get\set
...ToAdd toString() 利用Eclipse生产实例的字符串转换
...ToAdd Constructors from super 利用Eclipse生产默认构造函数
...ToAdd Constructor use fields 利用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; } }
身份验证处理,没有处理,有很多方法进行处理,根据项目情况实现它.
注意:1、每个action的请求方式都使用的method = RequestMethod.POST,是为了统一调用。
2、控制层,主要注意各种注解。
源代码:http://download.csdn.net/detail/lxhjh/5435319