标签:
Apache Wink就是一个纯Java的REST框架。它完整的实现了JSR 311并扩展了部分功能,此外还提供了良好的扩展性,难能可贵的是还可以与流行的Java框架Spring无缝集成。 1. 通过maven加载apache wink和spring整合所使用的包: 2、配置 Web Service当然是Web程序了,所以入口就是一个Servlet,在web.xml里面配置一下,把REST的访问都安排给Wink来处理: <servlet> 与Spring的集成,需要讲wink包里的wink-core-context.xml载入,配置如下 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:META-INF/server/wink-core-context.xml,classpath:config/spring/spring-config.xml</param-value> </context-param> spring配置相关的Resource: <bean class="org.apache.wink.spring.Registrar"> .....后面的定义就是大家比较熟悉的Resrouce定义 |
测试代码:
/*************************************公共连接分类****************************************/
/**
* 添加公共连接分类
* @param jsonObj 公共连接分类json数据
* @return 0:失败 1:成功 2:重复添加
* @throws UnsupportedEncodingException 异常
*/
@PUT
@Path("/add/commonlinktype")
@Produces ({MediaType.APPLICATION_JSON})
@AuthMethod
public JSONObject addCommonlinkType(JSONObject jsonObj) throws UnsupportedEncodingException{
JSONObject result = new JSONObject();
//0表示添加失败
result.put("result", 0);
if(null != jsonObj){
CommonlinkType commonlinkType = new CommonlinkType();
buildJSONObjectToCommonlinkType(commonlinkType,jsonObj);
//是否已经存在该连接
if(hasExistCommonlinkType(commonlinkType)){
//2表示存在
return result.put("result", 2);
}
boolean success = commonlinkService.addCommonlinkType(commonlinkType);
if(success){
//1表示添加成功
result.put("result", 1);
return result;
}
}
return result;
}
/**
* 删除公共连接分类
* @param id 分类id
* @return 0:失败 1:成功
*/
@DELETE
@Path("/delete/commonlinktype/{id}")
@Produces ({MediaType.APPLICATION_JSON})
public JSONObject deleteCommonlinkType(@PathParam("id") String id){
HttpSession session = request.getSession();
User user = (User)session.getAttribute("loginUser");
JSONObject result = new JSONObject();
result.put("result", 0);
if(null != user){
String userId = String.valueOf(user.getId());
CommonlinkType commonlinkType = new CommonlinkType(id,userId);
boolean success = commonlinkService.deleteCommonlinkType(commonlinkType);
if(success){
result.put("result", 1);
return result;
}
}
return result;
}
/**
* 更新公共链接分类
* @param jsonObj 公共连接分类json数据
* @return 0:失败 1:成功
*/
@POST
@Path("/update/commonlinktype")
@Produces ({MediaType.APPLICATION_JSON})
public JSONObject updateCommonlinkType(JSONObject jsonObj){
HttpSession session = request.getSession();
User user = (User)session.getAttribute("loginUser");
JSONObject result = new JSONObject();
result.put("result", 0);
if(null != jsonObj && null != user){
String id = jsonObj.optString("id");
String userId = String.valueOf(user.getId());
String name = jsonObj.optString("name");
CommonlinkType commonlinkType = new CommonlinkType(id,name,userId,DataUtil.date2Str(new Date()));
boolean success = commonlinkService.updateCommonlinkType(commonlinkType);
if(success){
result.put("result", 1);
return result;
}
}
return result;
}
/**
* 查询公共连接分类列表
* @param page 当前页
* @param pageNum 每页显示条数
* @return 公共连接分类列表json
*/
@GET
@Path("/list/commonlinktype")
@Produces ({MediaType.APPLICATION_JSON})
public JSONObject findCommonlinkTypeList( @DefaultValue("1") @QueryParam("page") int page, @DefaultValue("10") @QueryParam("pagenum") int pageNum){
JSONObject result = new JSONObject();
JSONArray jsonArray = new JSONArray();
result.put("count", 0);
result.put("commonlinktypelist", jsonArray);
HttpSession session = request.getSession();
User user = (User)session.getAttribute("loginUser");
if(null != user){
PageBounds pb = PageBoundsUtil.PageBoundsOrderExtend("orderId.desc");
pb.setPage(page);
pb.setLimit(pageNum);
CommonlinkType commonlinkType = new CommonlinkType(String.valueOf(user.getId()));
Pager<CommonlinkType> commonLinkPage = commonlinkService.findCommonlinkTypeList(commonlinkType,pb);
if(null != commonLinkPage && commonLinkPage.getTotal() > 0){
result.put("count",commonLinkPage.getTotal());
buildCommonlinkTypeListToJSONAarray(commonLinkPage,jsonArray);
}
}
return result;
}
/**
* 判断是否已经存在当前分类
* @param commonlinkType 分类参数
* @return true:表示存在 false:表示不存在
*/
private boolean hasExistCommonlinkType(CommonlinkType commonlinkType){
int count = commonlinkService.findCommonlinkTypeCountByName(commonlinkType);
if(count > 0){
return true;
}
return false;
}
private void buildJSONObjectToCommonlinkType(CommonlinkType commonlinkType, JSONObject jsonObj) throws UnsupportedEncodingException {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("loginUser");
commonlinkType.setId(Utils.getUuid());
commonlinkType.setName(URLDecoder.decode(jsonObj.optString("name"),"UTF-8"));
commonlinkType.setUserId(null != user ? String.valueOf(user.getId()) : "");
commonlinkType.setCreateDate(DataUtil.date2Str(new Date()));
commonlinkType.setLastmodifyDate(DataUtil.date2Str(new Date()));
}
private void buildCommonlinkTypeListToJSONAarray(Pager<CommonlinkType> commonlinkTypePage,JSONArray result){
if(null != commonlinkTypePage){
for(CommonlinkType commonlinkType : commonlinkTypePage.getDatas()){
JSONObject jsObj = new JSONObject();
jsObj.put("id", commonlinkType.getId());
jsObj.put("name", commonlinkType.getName());
jsObj.put("userId", commonlinkType.getUserId());
jsObj.put("createDate", commonlinkType.getCreateDate());
jsObj.put("lastmodifyDate", commonlinkType.getLastmodifyDate());
result.put(jsObj);
}
}
}
private JSONObject buildCommonlinkTypeJsonObj(JSONObject jsObj,CommonlinkType commonlinkType) {
jsObj.put("id", commonlinkType.getId());
jsObj.put("name", commonlinkType.getName());
jsObj.put("userId", commonlinkType.getUserId());
jsObj.put("createDate", commonlinkType.getCreateDate());
jsObj.put("lastmodifyDate", commonlinkType.getLastmodifyDate());
return jsObj;
}
private JSONObject buildCommonlinkJsonObj(JSONObject jsObj,Commonlink commonlink) {
jsObj.put("id", commonlink.getId());
jsObj.put("name", commonlink.getName());
jsObj.put("userId", commonlink.getUserId());
jsObj.put("createDate", commonlink.getCreateDate());
jsObj.put("lastmodifyDate", commonlink.getLastmodifyDate());
jsObj.put("url", commonlink.getUrl());
jsObj.put("position", commonlink.getPosition());
return jsObj;
}
附上rest服务测试界面,绝对可以使用到app应用:
各位有需要源代码的请联系扣扣:3121026417
springmvc + apache wink rest整合
标签:
原文地址:http://my.oschina.net/u/2349216/blog/407041