一个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
public void configConstant(ConstantLoader constantLoader) {
//通过后缀来返回不同的数据类型 你可以自定义自己的 render
//如:public class FreemarkerRender extends Render{}
//目前支持json,text,file三种输出
//constantLoader.addRender("ftl", new FreemarkerRender());
}
public void configResource(ResourceLoader resourceLoader) {
//设置resource的目录 Resty启动时通过扫描配置的目录来添加Resouce
//排除某个目录
//resourceLoader.addExcludePackages("cn.dreampie.resource");
//扫描某个目录
resourceLoader.addIncludePackages("cn.dreampie.resource");
}
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);
}
public void configInterceptor(InterceptorLoader interceptorLoader) {
//权限拦截器
//interceptorLoader.add(new SecurityInterceptor(2, new MyAuthenticateService()));
//Resource层事务的拦截器 @Transaction
//interceptorLoader.add(new TransactionInterceptor());
}
public void configHandler(HandlerLoader handlerLoader) {
//跨域
handlerLoader.add(new CORSHandler());
}
public void afterStart() {
//Resty启动前执行的操作
}
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/v1.0")
public class ApiResource extends Resource {
}
@API("/tests")
public class TestResource extends ApiResource {
@GET
public WebResult get() {
//如果需要返回请求状态 使用new WebResult
return new WebResult(HttpStatus.OK, Maper.of("a", "1", "b", "2"));
}
@GET("/:name")
public WebResult get(String name) {
//如果需要返回请求状态 使用new WebResult
return new WebResult(HttpStatus.OK, Maper.of("a", "1", "b", "2"));
}
@POST("/post")
public Map post(Map<String, String> test) {
return test;
}
}
以上就是搭建一个基础的Resty项目框架的详细内容,更多关于搭建Resty项目框架的资料请关注编程网其它相关文章!