Resteasy 的用法实例
RESTEasy 简介:
RESTEasy是JBoss的一个开源项目,提供各种框架帮助你构建RESTful Web Services和RESTful Java应用程序。它是JAX-RS规范的一个完整实现并通过JCP认证。作为一个JBOSS的项目,它当然能和JBOSS应用服务器很好地集成在一起。但是,它也能在任何运行JDK5或以上版本的Servlet容器中运行。RESTEasy还提供一个RESTEasy JAX-RS客户端调用框架。能够很方便与EJB、Seam、Guice、Spring和Spring MVC集成使用。支持在客户端与服务器端自动实现GZIP解压缩。
规范内容
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 inte易做图ce 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
补充:软件开发 , Java ,