代码拉取完成,页面将自动刷新
ditty是基于netty、fastjson实现的一款极致简约web restful框架。
/**
* 注册路由信息的类,类上需要有{@link Controller}注解
* @author dingnate
*
*/
@ActionInterceptor(ControllerInterceptor.class)
@ActionMapping(actionKey = "/controller")
@Controller
public class DemoController {
/**
* httpMethod指定http的请求方式
* argName指定参数名可以在web访问的时候使用<br>
* actionKey:控制器的actionKey和方法的actionKey确定该方法的唯一路由路径<br>
* 不指定方法的actionKey默认为方法名称
* @param b
* @return
*/
@ActionMapping(httpMethod = HttpKit.METHOD_POST, actionKey = "method1", argNames = { "b" })
public AxBean method1(BxBean b) {
System.out.println(getClass().getName() + ".method1(BBean)");
return new AxBean(1, true, "A", b);
}
/**
* 本方法的拦截器不仅包含在本方法上声明的拦截器,还包含上层控制器的拦截器、全局拦截器
* @param a
* @return
*/
@ActionInterceptor(MethodInterceptor.class)
@ActionMapping(argNames = { "a" })
public int method2(int a) {
System.out.println(getClass().getName() + ".method2(int)");
return a;
}
/**
* 这个方法清除了上层拦截器,保留本方法的拦截器<br>
* 通过web访问时,参数名可以用下表代替,比如 “a”->"0"
* @param a
* @return
*/
@ClearActionInterceptor
@ActionInterceptor(MethodInterceptor.class)
@ActionMapping
public String method3(String a) {
System.out.println(getClass().getName() + ".method3(int)");
return a;
}
/**
* 没有ActionMapping注解,这个方法不会被注册,无法通过web访问
*/
public void method4() {
System.out.println(getClass().getName() + ".method4()");
}
}
public class MethodInterceptor extends AbstractActionIntercptor {
@Override
public void interceptor(ActionInvoker invoker) {
System.out.println(getClass().getName()+":in");
invoker.inovke();
System.out.println(getClass().getName()+":out");
}
}
com.ditty.demo.server.ServerDemo.java
public class ServerDemo {
public static void main(String[] args) {
//注册单个Controller类,不开启类扫描器时使用
//HttpConfiguration.me().getRouters().add(Controller.class);
//开启类扫描器,配置类扫描器的class过滤前缀 和Jar包过滤前缀
HttpConfiguration.me().getClassSaner().addClassPrefixs("com.ditty.demo.").addJarPrefixs("").scanClass();
//开启类扫描器,不配置扫描前缀时为全量扫描,性能没有配置过滤前缀的块
//HttpConfiguration.me().getClassSaner().scanClass();
ServerFactory.me().getServer().start();
//自定义服务器启动
//ServerFactory.me().getServer().start(webDir, port, contextPath);
}
}
注:支持自定义类扫描,扫描到带Controller
注解类自动注册其路由信息
POST localhost:8090/controller/method1
{
"b":{"a":1,"b":true,"d":"2018-05-26 16:36:34.039","s":"ss"}
}
{
"returnValue": {
"a": 1,
"b": true,
"bBean": {
"a": 1,
"b": true,
"d": "2018-05-26 16:36:34.039",
"s": "ss"
},
"s": "A"
}
}
POST localhost:8090/controller/method2
{
"0":1
}
{
"returnValue": 1
}
POST localhost:8090/controller/method3/cc
注:路径参数使用方式:url路径尾部/
追加参数值,多个参数值以-
为分隔
POST localhost:8090/controller/method3?0=cc
{
"returnValue": "cc"
}
注:Query参数使用方式:url路径尾部?
追加参数赋值表达式arg0=arg0Value
,多个表达式以&
分隔
GET localhost:8090/index.html
注:静态资源必须包含.
2018-07-17
2018-06-22
2018-06-20
2018-05-31
2018-05-28
2018-05-27
请在下方评论或提issue,我会及时回复。
如果感觉本项目对您有用,打赏是对作者最大的鼓励。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。