规范内容
JAX-RS提供了一些标注将一个资源类,一个POJOJava类,封装为Web资源。
标注包括:
@Path,标注资源类或方法的相对路径
@GET,@PUT,@POST,@DELETE,标注方法是用的HTTP请求的类型
@Produces,标注返回的MIME媒体类型
@Consumes,标注可接受请求的MIME媒体类型
@PathParam,@QueryParam,@HeaderParam,@CookieParam,@MatrixParam,@FormParam,分别标注方法的参数来自于HTTP请求的不同位置,例如@PathParam来自于URL的路径,@QueryParam来自于URL的查询参数,@HeaderParam来自于HTTP请求的头信息,@CookieParam来自于HTTP请求的Cookie。
下面使用iplat4j+Spring+ibatiS架构平台和MiniUI 前台技术实现一个简单删除操作。
第一:配置web.xml
<servlet> <servlet-name>Resteasy</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> </servlet> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/restservice/*</url-pattern> </servlet-mapping>
第二:Rest*.java类的编写
/** * 数据订阅 Rest * @author Administrator * */ @SuppressWarnings(value={"rawtypes"}) @Path("restservice") public class RestSubscribe { Logger logger = Logger.getLogger(RestUser.class); @POST @Path("/deleteSubscribe2") @Produces("application/json; charset=utf-8") /** * 删除数据订阅 * 根据 consumerid和categoryId 删除 * @param request * @return */ public String deleteSubscribe2(@Context HttpServletRequest request){ EiInfo inInfo = new EiInfo(); String data = request.getParameter("data"); String consumerId = request.getParameter("consumerId"); String operator = request.getParameter("operator"); inInfo.setCell("i", 0, "consumerId", consumerId); inInfo.setCell("i", 0, "operator", operator); inInfo.setCell("i", 0, "categoryIds", data); inInfo.set(EiConstant.serviceName, "FBSubscribe"); inInfo.set(EiConstant.methodName, "deleteSubscribe2"); inInfo = SoaManager.callNewTx(inInfo); Map<String, Object> map = new HashMap<String, Object>(); map.put("result", "删除成功"); return JSONUtil.getInstance(). object2JSON(map, JSONUtil.Feature.DEFAULT_DATE_PATTERN); } }
第三:service类的编写
/** * 数据订阅服务类 * @author Administrator * */ @SuppressWarnings(value={"rawtypes","unchecked"}) public class ServiceFBSubscribe extends ServiceBase{ Logger logger = Logger.getLogger(ServiceFBSubscribe.class); SubscribeRepository subscribeRep = (SubscribeRepository) SpringApplicationContext.getBean("subscribeRep"); /** * 根据 consumerId和 categoryId * 删除数据订阅 * @param user */ public EiInfo deleteSubscribe2(EiInfo inInfo){ Map<String,String> resultMap = new HashMap<String,String>(); try { String categoryId = (String)inInfo.getCell("i", 0, "categoryIds"); String consumerId=(String)inInfo.getCell("i", 0, "consumerId"); String operator=(String)inInfo.getCell("i", 0, "operator"); String[] categoryIds = categoryId.split(","); Subscribe subscribe = new Subscribe(); for(String id : categoryIds){ subscribe.setCategoryId(id); subscribe.setConsumerId(consumerId); subscribe.setGuid(id); subscribeRep.delete2(subscribe); } //打印服务器返回的状态 resultMap.put("message", "删除成功"); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); inInfo.setStatus(EiConstant.STATUS_FAILURE); inInfo.setMsg("删除数据订阅失败!"); resultMap.put("result", "false"); resultMap.put("message", "删除数据订阅失败!"); } return inInfo; } }
第四:管理仓储类(*Repository.java)的编写
1.接口类:
public interface SubscribeRepository { /** * 删除数据推送反馈 * @param user */ public void delete2(Subscribe subscribe); }
2,实现类:
/** * 数据订阅维护仓储 * @author:hushiguo * @date:2012-11-19下午2:59:15 */ @SuppressWarnings({ "unchecked", "rawtypes", "unused"}) public class SubscribeRepositoryImpl extends RepositoryBase implements SubscribeRepository{ public void delete2(Subscribe subscribe) { // TODO Auto-generated method stub if (subscribe.getConsumerId() == null || "".equals(subscribe.getConsumerId())) throw new AppException("数据订阅不存在!"); try{ DaoUtil.delete(dao, "FBSubscribe.delete2", subscribe); }catch(Exception e){ throw new AppException("删除数据订阅失败!"); } } }
第五:spring注册仓储类(applicationContext-fb-persist.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="subscribeRep" class="com.baosight.buap.fb.persist.SubscribeRepositoryImpl"> <property name="dao" ref="dao" /> </bean> </beans>
第六:Rest类注册(resteasy.properties)
com.baosight.buap.fb.rest.RestSubscribe
第七 :ibatIS 的 *sql.xml 编写( FBSubscribe.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap namespace="FBSubscribe"> <delete id="delete2" > DELETE FROM T_UDP_SUBSCRIBE WHERE CATEGORY_ID=#categoryId# andCONSUMER_ID=#consumerId# </delete> </sqlMap>
第八:Jsp页面(subscribeManage.jsp)
<body> <div id="dept_grid" class="mini-datagrid" style="width: 100%; height: 180px;" url="<%=path %>/restservice/pQueryConsumers" idField="appCode" onselectionchanged="onSelectionChanged" selectOnLoad="true"> <div property="columns"> <div field="appName" width="100" headerAlign="center" allowSort="true"> 客户名称 <input property="editor" class="mini-textbox" style="width: 100%;" /> </div> <div field="appDesc" width="300" headerAlign="center" allowSort="true"> 客户描述 <input property="editor" class="mini-textbox" style="width: 100%;" /> </div> </div> </div> <br /> <div id="employee_grid" class="mini-datagrid" multiSelect="true" style="width: 100%; height: 250px;" url="<%=path %>/restservice/queryByConsumerId2" idField="guid" > <div property="columns"> <div type="checkcolumn"></div> <div field="categoryCode" width="100" headerAlign="center" allowSort="true"> 数据类别代码 <input property="editor" class="mini-textbox" style="width: 100%;" /> </div> <div field="categoryName" width="100" headerAlign="center" allowSort="true"> 数据类别名称 <input property="editor" class="mini-textbox" style="width: 100%;" /> </div> <div field="status" width="100" headerAlign="center" allowSort="true"> 状态 <input property="editor" class="mini-textbox" style="width: 100%;" /> </div> </div> </div> <br /> <a class="mini-button" style="margin-left: 70%;" iconCls="icon-edit" onclick="updateType('0')">启动</a> <a class="mini-button" style="margin-left: 2%;" iconCls="icon-edit" onclick="updateType('1')">停止</a> <a class="mini-button" style="margin-left: 2%;" iconCls="icon-remove" onclick="delType">删除</a> <script type="text/javascript"> mini.parse(); var dept_grid = mini.get("dept_grid"); var employee_grid = mini.get("employee_grid"); //加载列表 dept_grid.load(); // 联动查询 function onSelectionChanged(e) { var grid = e.sender; var record = grid.getSelected(); if (record) { employee_grid.load({"consumerId":record.guid}); } } //删除 数据订阅 function delType(){ var row = dept_grid.getSelected(); var rows = employee_grid.getSelecteds(); var categoryId = []; if(rows.length<1){ alert('请至少选择一个数据类别!'); return; }else{ for(var i = 0;i < rows.length;i++){ categoryId.push(rows[i].categoryCode); } categoryId = categoryId.join(","); } if(rows.length > 0){ if(confirm("确定要删除吗?")){ //var json = {categoryId : categoryId}; //json = mini.encode(json); var consumerId=row.appCode; $.ajax({ url: "<%=path %>/restservice/deleteSubscribe2/", data: { data: categoryId,consumerId :consumerId ,operator:operator }, type:"post", success: function (text) { alert(text.result); location.reload(); }, error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR.responseText); } }); } } } </script> </bdoy>
第九:JSP页面展示