rest插件介绍
是基于http服务的rest插件,提供了很简单的rest服务接口方便开发。rest根据加载的service组件自动生成对应的httpservlet。其接口比spring boot之类的rest服务框架要简化得多,不需要进行注解配置也可以按rest服务加载。
快速上手
为了让rest生效,必须配置 中 http 的
同时必须在
port="5001">
protocol="http" port="6060" nodeinterceptor="org.redkalex.rest.restnodeinterceptor">
base="org.redkalex.test.rest.simplerestservlet" mustsign="false" autoload="true" includes="" excludes="">
value="com.xxx.xxxxservice"/>
通常配置都需要编写一个 org.redkalex.rest.resthttpservlet 子类,主要用于获取当前用户信息和鉴权,且必须指定具体的user对象类。开发者的实现类可以参考 中的baseservlet类,以下是一个简单的范例:
public class simplerestservlet extends resthttpservlet<userinfo> {
@resource
private userservice userservice;
//获取当前用户信息
@override
protected userinfo currentuser(httprequest req) throws ioexception {
string sessionid = req.getsessionid(false);
if (sessionid == null || sessionid.isempty()) return null;
return userservice.current(sessionid);
}
//普通鉴权
@override
public boolean authenticate(int module, int actionid, httprequest request, httpresponse response) throws ioexception {
userinfo info = currentuser(request);
if (info == null) {
response.addheader("retcode", retcodes.ret_user_unlogin);
response.addheader("retmessage", "not login");
response.setstatus(203);
response.finish("{'success':false, 'message':'not login'}");
return false;
} else if (!info.checkauth(module, actionid)) { // 根据module、actionid进行鉴权
response.addheader("retcode", retcodes.ret_user_auth_illegal);
response.addheader("retmessage", "no authority");
response.setstatus(203);
response.finish("{'success':false, 'message':'no authority'}");
return false;
}
return true;
}
}
编写完 org.redkalex.rest.resthttpservlet 子类后就需要对service进行设置,设置需要三大注解:@restcontroller、@restmapping、@restparam。
@restcontroller :
/**
* 只能依附在service类上,value默认为service的类名去掉service字样的字符串 (如helloservice,的默认路径为 hello)。
*/
@target({type})
public @interface restcontroller {
boolean ignore() default false; //是否屏蔽该类的转换
string value() default ""; //模块名, 只能是模板名,不能含特殊字符
boolean repair() default true; //同@webservlet的repair属性
int module() default 0; //模块id值,鉴权时用到
}
@restmapping :
/**
* 只能依附在service实现类的public方法上
* value默认为"/" service的类名去掉service及后面字样的小写字符串 (如helloservice,的默认路径为/hello)。
*/
@target({method})
public @interface restmapping {
boolean ignore() default false; //是否屏蔽该方法的转换
//请求的方法名, 不能含特殊字符
//默认为方法名的小写(若方法名以createxxx、updatexxx、deletexxx、queryxxx、findxxx且xxxservice为service的类名将只截取xxx之前)
string name() default "";
boolean authignore() default true; //是否跳过鉴权,默认跳过
int actionid() default 0; //操作id值,鉴权时用到
string contenttype() default ""; //设置response的contenttype 默认值为 text/plain; charset=utf-8
string jsvar() default ""; //以application/javascript输出对象是指明js的对象名,该值存在时则忽略contenttype()的值
}
@restparam :
/**
* 只能依附在service类的方法的参数上, http请求的参数名
*/
@target({parameter})
public @interface restparam {
string value(); //参数名
}
rest的设置方式有两大种: 一种是采用默认rest注解,一种是显式的设置。
第一种方式需要开发者对service中的方法命名需要遵循一定规则, 如下范例,模块为hello,service类命名为helloservice,增删改查的方法采用createhello、deletehello、updatehello、queryhello或其他xxxhello命名。rest插件加载任何没有标记@restcontroller、@restmapping、@restparam 的service将按照一定规则生成默认值:
1、@restcontroller.value() 默认值为service类名的小写化并去掉service及后面字样, 视为模块名
2、@restmapping.name() 默认值为service的方法名小写化并去掉模块名字样
2、@restparam.value() 如果方法名以find、delete开头且方法的参数只有一个且参数类型是基本数据类型或string,则默认值为"#";若使用java 8中带上 -parameters 编译项的新特性,默认值为参数名, 若没使用新特性则采用bean、bean2、bean3...的命名规则。
/**
* 类说明:
* flipper : source组件中的翻页对象, 只要service方法中的参数类与该类相同,则不需要设定 @restparam
* userinfo :当前用户类, 只要service方法中的参数类与该类相同,则不需要设定 @restparam
* helloentity: hello模块的实体类
* hellobean: hellow模块实现filterbean的过滤bean类
*
*/
@restcontroller(value = "hello", module = 0, repair = true, ignore = false)
public class helloservice implements service {
@resource
private datasource source;
//增加记录
@restmapping(name = "create", authignore = true)
public retresult<helloentity> createhello(userinfo info, @restparam("bean") helloentity entity) { //通过 /hello/create?bean={...} 增加对象
entity.setcreator(info == null ? 0 : info.getuserid()); //设置当前用户id
entity.setcreatetime(system.currenttimemillis());
source.insert(entity);
return new retresult<>(entity);
}
//删除记录
@restmapping(name = "delete", authignore = true)
public void deletehello(@restparam("#") int id) { //通过 /hello/delete/1234 删除对象
source.delete(helloentity.class, id);
}
//修改记录
@restmapping(name = "update", authignore = true)
public void updatehello(@restparam("bean") helloentity entity) { //通过 /hello/update?bean={...} 修改对象
entity.setupdatetime(system.currenttimemillis());
source.update(entity);
}
//查询列表
@restmapping(name = "query", authignore = true)
public sheet<helloentity> queryhello(@restparam("bean") hellobean bean, flipper flipper) { //通过 /hello/query/start:0/size:20?bean={...} 查询列表
return source.querysheet(helloentity.class, flipper, bean);
}
//查询单个
@restmapping(name = "find", authignore = true)
public helloentity findhello(@restparam("#") int id) { //通过 /hello/find/1234 查询对象
return source.find(helloentity.class, id);
}
}
根据默认命名规则可以看出,以上范例生成的restservlet与去掉所有@restcontroller、@restmapping、@restparam后的service生成的是完全相同的。 rest插件根据service会动态生成httpservlet,以上范例生成的httpservlet如下:
@webservlet(value = {"/hello/*"}, repair = true)
public class _dynhellorestservlet extends simplerestservlet {
@resource
private helloservice _service;
@authignore
@webaction(url = "/hello/create")
public void create(httprequest req, httpresponse resp) throws ioexception {
helloentity bean = req.getjsonparameter(helloentity.class, "bean");
userinfo user = currentuser(req);
retresult<helloentity> result = _service.createhello(user, bean);
sendretresult(resp, result);
}
@authignore
@webaction(url = "/hello/delete/")
public void delete(httprequest req, httpresponse resp) throws ioexception {
int id = integer.parseint(req.getrequsturilastpath());
_service.deletehello(id);
sendretresult(resp, retresult.success);
}
@authignore
@webaction(url = "/hello/update")
public void update(httprequest req, httpresponse resp) throws ioexception {
helloentity bean = req.getjsonparameter(helloentity.class, "bean");
_service.updatehello(bean);
sendretresult(resp, retresult.success);
}
@authignore
@webaction(url = "/hello/query")
public void query(httprequest req, httpresponse resp) throws ioexception {
hellobean bean = req.getjsonparameter(hellobean.class, "bean");
flipper flipper = findflipper(req);
sheet<helloentity> result = _service.queryhello(bean, flipper);
resp.finishjson(result);
}
@authignore
@webaction(url = "/hello/find/")
public void find(httprequest req, httpresponse resp) throws ioexception {
int id = integer.parseint(req.getrequsturilastpath());
helloentity bean = _service.findhello(id);
resp.finishjson(bean);
}
}
rest的第二种设置方式是显式的设置, 看如下范例:
/**
* 类说明:
* flipper : source组件中的翻页对象
* userinfo :当前用户类
* helloentity: hello模块的实体类
* hellobean: hellow模块实现filterbean的过滤bean类
*
*/
public class helloservice implements service {
@resource
private datasource source;
//增加记录
public retresult<helloentity> createhello(userinfo info, helloentity entity) {
entity.setcreator(info == null ? 0 : info.getuserid()); //设置当前用户id
entity.setcreatetime(system.currenttimemillis());
source.insert(entity);
return new retresult<>(entity);
}
//删除记录
public void deletehello(int id) { //通过 /hello/delete/1234 删除对象
source.delete(helloentity.class, id);
}
//修改记录
public void updatehello(helloentity entity) { //通过 /hello/update?bean={...} 修改对象
entity.setupdatetime(system.currenttimemillis());
source.update(entity);
}
//修改记录
@restmapping(name = "partupdate") //不能使用updatepart,因为存在update,是updatepart的开头部分,不符合basedhttpservlet的webaction规则
public void updatehello(helloentity entity, @restparam("cols") string[] columns) { //通过 /hello/update?bean={...} 修改对象
entity.setupdatetime(system.currenttimemillis());
source.updatecolumns(entity, columns);
}
//查询sheet列表
public sheet<helloentity> queryhello(hellobean bean, flipper flipper) { //通过 /hello/query/start:0/size:20?bean={...} 查询sheet列表
return source.querysheet(helloentity.class, flipper, bean);
}
//查询list列表
@restmapping(name = "list")
public list<helloentity> queryhello(hellobean bean) { //通过 /hello/list?bean={...} 查询list列表
return source.querylist(helloentity.class, bean);
}
//查询单个
@restmapping(name = "find")
@restmapping(name = "jsfind", jsvar = "varhello")
public helloentity findhello(@restparam("#") int id) { //通过 /hello/find/1234 查询对象
return source.find(helloentity.class, id);
}
}
转换为resthttpservlet如下:
@webservlet(value = {"/hello/*"}, repair = true)
public class _dynhellorestservlet extends simplerestservlet {
@resource
private helloservice _service;
@authignore
@webaction(url = "/hello/create")
public void create(httprequest req, httpresponse resp) throws ioexception {
helloentity bean = req.getjsonparameter(helloentity.class, "bean");
userinfo user = currentuser(req);
retresult<helloentity> result = _service.createhello(user, bean);
sendretresult(resp, result);
}
@authignore
@webaction(url = "/hello/delete/")
public void delete(httprequest req, httpresponse resp) throws ioexception {
int id = integer.parseint(req.getrequsturilastpath());
_service.deletehello(id);
sendretresult(resp, retresult.success);
}
@authignore
@webaction(url = "/hello/update")
public void update(httprequest req, httpresponse resp) throws ioexception {
helloentity bean = req.getjsonparameter(helloentity.class, "bean");
_service.updatehello(bean);
sendretresult(resp, retresult.success);
}
@authignore
@webaction(url = "/hello/partupdate")
public void partupdate(httprequest req, httpresponse resp) throws ioexception {
helloentity bean = req.getjsonparameter(helloentity.class, "bean");
string[] cols = req.getjsonparameter(string[].class, "cols");
_service.updatehello(bean, cols);
sendretresult(resp, retresult.success);
}
@authignore
@webaction(url = "/hello/query")
public void query(httprequest req, httpresponse resp) throws ioexception {
hellobean bean = req.getjsonparameter(hellobean.class, "bean");
flipper flipper = findflipper(req);
sheet<helloentity> result = _service.queryhello(bean, flipper);
resp.finishjson(result);
}
@authignore
@webaction(url = "/hello/list")
public void list(httprequest req, httpresponse resp) throws ioexception {
hellobean bean = req.getjsonparameter(hellobean.class, "bean");
list<helloentity> result = _service.queryhello(bean);
resp.finishjson(result);
}
@authignore
@webaction(url = "/hello/find/")
public void find(httprequest req, httpresponse resp) throws ioexception {
int id = integer.parseint(req.getrequsturilastpath());
helloentity bean = _service.findhello(id);
resp.finishjson(bean);
}
@authignore
@webaction(url = "/hello/jsfind/")
public void jsfind(httprequest req, httpresponse resp) throws ioexception {
int id = integer.parseint(req.getrequsturilastpath());
helloentity bean = _service.findhello(id);
sendjsresult(resp, "varhello", bean);
}
}
rest插件让开发者省去了编写httpservlet过程,让开发更加敏捷。
相关推荐
以 convention 插件为基础,struts 2.1 又新增了 rest 插件,允许 struts 2 应用对外提供 rest 服务。rest 插件也无需使用 xml 进行配置管理。struts 2.1 通过 rest 插件完全可以提供让人和机器客户端共同使用的资源...
chrome插件advanced rest client来调试restful chrome插件advanced rest client来调试restful 可在360浏览器使用
struts2.1.6 convertion插件(即注释方式配置)的hello...rest插件例子 默认调用 create()方法 struts2.1.6 vistor校验例子 都是我测试例子,写到一块了 有点乱 哪为高手给我说下 rest-plugin有什么好处 项目中有说明文件
安装 rest 插件非常简单,只需按如下步骤进行即可: 将 struts 2 项目下 struts2-convention-plugin-2.1.6.jar、struts2-rest-plugin-2.1.6.jar 两个 jar 包复制到 web 应用的 web-inf/lib 路径下。 由于 struts 2 ...
chrome基于rest的web服务客户端插件.zip
google simple rest client 测试rest 服务等功能,压缩包内包含使用文档和安装文档
谷歌安装rest插件以便通过浏览器直接调试java后台请求
rest client(rest接口调试工具,有保存功配置功能) chrome浏览器插件 直接避免google不能访问问题 使用方法: http://www.cnblogs.com/jifeng/p/4429123.html
今天给大家介绍的这款网页调试工具不仅可以调试简单的css、html、脚本等简单的网页基本信息,它还可以发送几乎所有类型的http请求!postman在发送网络http请求方面可以说是chrome插件类产品中的代表产品之一。
谷歌插件-advanced rest client 和被国内墙所挡住的definitions.json
谷歌浏览器插件 advancedrestclient http rest api测试调试
谷歌插件advanced-rest-client 谷歌插件advanced-rest-client 谷歌插件advanced-rest-client 谷歌插件advanced-rest-client
dhc-rest-http-api-client,dhc是一款可以帮助用户使用chrome插件模拟http客户端发送测试数据到服务器的谷歌浏览器插件。 在chrome中安装了dhc插件以后,就可在服务器端代码初步完成的时候,使用dhc进行数据测试,...
直接放到tomcat目录就可以执行,详细的说明请到http://blog.csdn.net/baozhiyao234 看关于rest的说明,开发种经常用到
rest接口调试工具-chrome浏览器插件 rest接口调试工具-chrome浏览器插件
用于谷歌客户端调试开发接口api的方便工具,只需拖拽到谷歌浏览器即可
用于cakephp的rest插件 该插件简化了cakephp 3应用程序的rest api开发。 它只是将控制器的输出转换为json响应。 安装 您可以使用将此插件安装到cakephp应用程序中。 推荐的安装作曲家软件包的方法是: composer ...
dhc-rest-http-api-client(chrome插件),进入chrmoe插件开发模式,拖动进去便可自动安装。
适用于谷歌浏览器的http api客户端插件,安装该插件后可直接用谷歌浏览器进行http api请求,web应用开发快捷工具
rest的介绍,来源,限制条件,约束规则等方面的分析和学习。