文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
怎么搭建一个基础的 Resty 项目?
一个 Resty 项目包含的部分 (resty-route):
1. RestFilter:像其他 web 框架一样,Resty 也需要一个入口,在 web.xml 里配置,和常规的 Filter 区别不大
<filter>
<filter-name>RestyFilter</filter-name>
<filter-class>cn.dreampie.route.RestyFilter</filter-class>
<init-param>
<param-name>exclusions</param-name>
<!--把不经过 Resty 过滤的文件或者目录配置在这儿,逗号分割-->
<param-value>/**/*.js,/**/*.gif,/**/*.jpg,/**/*.png,/**/*.css,/**/*.ico,/druid/**</param-value>
</init-param>
<init-param>
<!--Resty 的核心配置文件,看第 2 点-->
<param-name>configClass</param-name>
<param-value>cn.dreampie.config.AppConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>RestyFilter</filter-name>
<!--过滤的路径-->
<url-pattern>/*</url-pattern>
</filter-mapping>
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 Render{}
//目前支持 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<String, String> test) {
return test;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论