这部分的代码在 java
http://git.oschina.net/terrymanu/miracle-framework/tree/master/miraclesea/framework-webmvc git
这个部分写的比较晚了,原本是设计的时候应该写的,可是由于当时写这个项目的时候,还没想好须要往这个模块中放什么。如今随着rbac模块的开发,渐渐的想到了一些能够提取出来的东西。 web
目前初版,只支持rest api,之后会考虑支持freemarker。 spring
1. 首先是一个流程。基本的CRUD都有一些标准流程。 数据库
如,增长一个实体的时候,首先要validate这个实体,其次经过业务方法从数据库中查询,判断这个实体是否打破了业务规则(如:惟一的用户名),若是失败,则返回相应的错误代码,若是成功。则返回正确的。 api
2. 基于这个流程,须要定义一个BaseResponse,用于封装Http状态码和业务对象。springmvc虽然提供了ResponseEntity,可是并无能够放入error以及warn的地方。因此须要扩展一下。 mvc
因此如今新增长了两个包,com.miraclesea.webmvc.vo,用于存放BaseResponse;com.miraclesea.webmvc.controller用于存放模板逻辑。 ui
其中核心的代码在AsyncBaseMangementController这个类中。提供了模板以及回调的接口。代码以下: spa
public abstract class AsyncBaseMangementController<T> { public final ResponseEntity<BaseResponse<T>> add(final T entity, final BindingResult result, final MvcCallback callback) { BaseResponse<T> response = new BaseResponse<T>(); if (result.hasErrors()) { for (ObjectError error : result.getAllErrors()) { response.getErrors().add(ResponseFeedback.builder().code(error.getCodes()[0]).build()); } return new ResponseEntity<BaseResponse<T>>(response, HttpStatus.UNPROCESSABLE_ENTITY); } try { callback.execute(); } catch (final DataIntegrityViolationException ex) { response.getErrors().add(ResponseFeedback.builder().code(ex.getKey()).build()); return new ResponseEntity<BaseResponse<T>>(response, HttpStatus.CONFLICT); } return new ResponseEntity<BaseResponse<T>>(response, HttpStatus.CREATED); } }
如今有一些问题未肯定。若是是建立成功,返回201,这个是没问题的。若是建立失败,验证失败返回422,资源冲突返回409,这两个有点拿不许。若是哪位熟悉http协议请告知,很是感谢。 .net
另外定义了一个回调接口,业务方法就写在这个回调接口中。
修改和删除未完待续