搭建一个基础的Resty项目框架

网友投稿 264 2022-08-23


搭建一个基础的Resty项目框架

目录1.RestFilter2.Config3.resources4.编写一个Resource

一个Resty项目包含的部分(resty-route):

1. RestFilter

像其他web框架一样,Resty也需要一个入口,在web.xml里配置,和常规的Filter区别不大

RestyFilter

cn.dreampie.route.RestyFilter

exclusions

/**/*.js,/**/*.gif,/**/*.jpg,/**/*.png,/**/*.css,/**/*.ico,/druid/**

configClass

cn.dreampie.config.AppConfig

RestyFilter

/*

2. Config

Resty的核心配置文件,你需要自己编写一个Config的java文件,并继承Config类,参考如下,根据自己的实际情况选择配置

public class AppConfig extends Config {

/**

* Config constant

* 配置常量,目前只能配置render,其他全局配置会自动读取根目录下的application.properties文件

*/

public void configConstant(ConstantLoader constantLoader) {

//通过后缀来返回不同的数据类型 你可以自定义自己的 render

//如:public class FreemarkerRender extends RenderaYYkUeBCjq{}

//目前支持json,text,file三种输出

//constantLoader.addRender("ftl", new FreemarkerRender());

}

/**

* Config resource

* 配置Resource的扫描目录

*/

public void configResource(ResourceLoader resourceLoader) {

//设置resource的目录 Resty启动时通过扫描配置的目录来添加Resouce

//排除某个目录

//resourceLoader.addExcludePackages("cn.dreampie.resource");

//扫描某个目录

resourceLoader.addIncludePackages("cn.dreampie.resource");

}

/**

* Config plugin

* 配置插件 数据库相关配置信息和constant一样 都会自动读取application.properties文件

*/

public void configPlugin(PluginLoader pluginLoader) {

//第一个数据源 使用c3p0连接池,数据源名字 default

//C3p0DataSourceProvider cdsp = new C3p0DataSourceProvider("default");

//activerecord 初始化会从数据库获取表的元数据 并映射到class

//ActiveRecordPlugin activeRecordCdsp = new ActiveRecordPlugin(cdsp, true);

//排除的model目录

//activeRecordCdsp.addExcludePackages("cn.dreampie.orm");

//扫描的model目录

//activeRecordCdsp.addIncludePackages("cn.dreampie.orm");

//pluginLoader.add(activeRecordCdsp);

//第二个数据源 使用druid连接池 数据源名字 demo

//DruidDataSourceProvider ddsp = new DruidDataSourceProvider("demo");

//ActiveRecordPlugin activeRecordDdsp = new ActiveRecordPlugin(ddsp, true);

//pluginLoader.add(activeRecordDdsp);

//jndi数据源 数据源名字jndiDs

//JndiDataSourceProvider jdsp=new JndiDataSourceProvider("jndiDs","jndiName");

//ActiveRecordPlugin activeRecordJdsp = new ActiveRecordPlugin(ddsp, true);

//pluginLoader.add(activeRecordJdsp);

}

/**

* Config interceptor applied to all actions.

* 全局拦截,会在进入路由后,执行方法前执前

*/

public void configInterceptor(InterceptorLoader interceptorLoader) {

//权限拦截器

//interceptorLoader.add(new SecurityInterceptor(2, new MyAuthenticateService()));

//Resource层事务的拦截器 @Transaction

//interceptorLoader.add(new TransactionInterceptor());

}

/**

* Config handler

* 全局的拦截,会在进入路由前执行

*/

public void configHandler(HandlerLoader handlerLoader) {

//跨域

handlerLoader.add(new CORSHandler());

}

/**

* Call back after Resty start

*/

public void afterStart() {

//Resty启动前执行的操作

}

/**

* Call back before Resty stop

*/

public void beforeStop() {

//Resty停止前执行的操作

}

3. resources

application.properties 系统全局的配置信息

#not must auto load

app.encoding=UTF-8

app.devMode=true

app.showRoute=false

app.cacheEnabled=true

app.cacheManager=cn.dreampie.cache.redis.RedisCacheManager

##druid plugin auto load

db.default.url=jdbc:mysql://127.0.0.1/example?useUnicode=true&characterEncoding=UTF-8

db.default.user=dev

db.default.password=dev1010

db.default.dialect=mysql

#c3p0配置

c3p0.default.minPoolSize=3

c3p0.default.maxPoolSize=20

#druid配置

#druid.default.initialSize=10

#druid.default.maxPoolPreparedStatementPerConnectionSize=20

#druid.default.timeBetweenConnectErrorMillis=1000

#druid.default.filters=slf4j,stat,wall

#flyway database migration auto load

flyway.default.valid.clean=true

flyway.default.migration.auto=true

flyway.default.migration.initOnMigrate=true

db.demo.url=jdbc:mysql://127.0.0.1/demo?useUnicode=true&characterEncoding=UTF-8

db.demo.user=dev

db.demo.password=dev1010

db.demo.dialect=mysql

#druid

druid.demo.initialSize=10

druid.demo.maxPoolPreparedStatementPerConnectionSize=20

druid.demo.timeBetweenConnectErrorMillis=1000

druid.demo.filters=slf4j,stat,wall

#flyway

flyway.demo.valid.clean=true

flyway.demo.migration.auto=true

flyway.demo.migration.initOnMigrate=true

4. 编写一个Resource

如果我们需要在url中体现版本推荐首先创建一个ApiResource的基础Resource的java类

/**

* 通过@API 来知道api的url,如果子对象也使用了@API注解,他们的url会拼接

*/

@API("/api/v1.0")

public class ApiResource extends Resource {

/**

* 基础的api Resource 用来添加基础的路径或版本号 和一些公用方法

*/

}

/**

*上面说到@API会被拼接,那么TestResource的api就是 /api/v1.0/tests

*/

@API("/tests")

public class TestResource extends ApiResource {

/**

* @GET 没有指定url时,访问改方法的api为 GET /api/v1.0/tests

*/

@GET

public WebResult get() {

//如果需要返回请求状态 使用new WebResult

return new WebResult(HttpStatus.OK, Maper.of("a", "1", "b", "2"));

}

/**

* @GET("/:name") 或 @GET("/{name}") 来设置url参数

*/

@GET("/:name")

public WebResult get(String name) {

//如果需要返回请求状态 使用new WebResult

return new WebResult(HttpStatus.OK, Maper.of("a", "1", "b", "2"));

}

/**

* 访问改方法的api为 POST /api/v1.0/tests/post

* 传入参数时 test=json字符串

*/

@POST("/post")

public Map post(Map test) {

return test;

}

}

以上就是搭建一个基础的Resty项目框架的详细内容,更多关于搭建Resty项目框架的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【零基础学Python】后端开发篇 第二十一节--Python Web开发二:Django的安装和运行(前端开发python)
下一篇:#yyds干货盘点#django学习之视图
相关文章

 发表评论

暂时没有评论,来抢沙发吧~