極簡(jiǎn)的Resty服務(wù)端和客戶端RESTful框架
如果你還不是很了解restful,或者認(rèn)為restful只是一種規(guī)范不具有實(shí)際意義,推薦一篇osc兩年前的文章:RESTful API 設(shè)計(jì)最佳實(shí)踐 和 Infoq的一篇極其理論的文章 理解本真的REST架構(gòu)風(fēng)格 雖然有點(diǎn)老,介紹的也很簡(jiǎn)單,大家權(quán)當(dāng)了解,restful的更多好處,還請(qǐng)google
擁有jfinal/activejdbc一樣的activerecord的簡(jiǎn)潔設(shè)計(jì),使用更簡(jiǎn)單的restful框架
部分設(shè)計(jì)也來自jfinal+activejdbc+restx,也希望大家多多支持開源,開源不比較框架之間的優(yōu)劣,只描述自己的想法。
restful的api設(shè)計(jì),是作為restful的服務(wù)端最佳選擇(使用場(chǎng)景:客戶端和服務(wù)端解藕,用于對(duì)靜態(tài)的html客戶端(mvvm等),ios,andriod等提供服務(wù)端的api接口)
下載jar包: Resty jar
maven使用方式:
1. 添加maven snapshots倉庫
<repositories>
<repository>
<id>oss-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>2. 添加依賴包
<dependency>
<groupId>cn.dreampie</groupId>
<artifactId>resty-route</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>重大更新:
Record的時(shí)代已經(jīng)到來,你完全不用使用任何的model來執(zhí)行你的數(shù)據(jù)
//創(chuàng)建record的執(zhí)行器 針對(duì)sec_user表 并開啟緩存
Record recordDAO = new Record("sec_user",true);
//使用當(dāng)前數(shù)據(jù)源和表數(shù)據(jù) new一個(gè)對(duì)象來保存數(shù)據(jù)
recordDAO.reNew().set("屬性", "值").save();
Record r1 = recordDAO.reNew().set("屬性", "值");
Record r2 = recordDAO.reNew().set("屬性", "值");
//批量保存
recordDAO.save(r1, r2);
//更新
r2.set("屬性", "值").update()
//查詢?nèi)?
List<Record> records = recordDAO.findAll();
//條件查詢
recordDAO.findBy(where,paras)
//分頁查詢
Page<Record> records = recordDAO.paginateAll();
//根據(jù)id刪除
recordDAO.deleteById("1");
//本次查詢放棄使用cache
recordDAO.unCache().findBy(where,paras);
//把record的數(shù)據(jù)源切換到dsName數(shù)據(jù)源上
recordDAO.useDS(dsName).findBy(where,paras);等等,完全擺脫model,實(shí)現(xiàn)快速操作數(shù)據(jù)
Model支持動(dòng)態(tài)切換數(shù)據(jù)源和本次查詢放棄使用cache
User dao=new User(); //本次查詢放棄使用cache dao.unCache().findBy(where,paras); //把model的數(shù)據(jù)源切換到dsName數(shù)據(jù)源上 dao.useDS(dsName).findBy(where,paras);
數(shù)據(jù)庫和全局參數(shù)配置移植到application.properties 詳情參看resty-example
//not must auto load
app.encoding=UTF-8
app.devMode=true
app.showRoute=true
app.cacheEnabled=true
//druid plugin auto load
//dsName is "default" you can use everything
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
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
//數(shù)據(jù)庫的配置精簡(jiǎn) 自動(dòng)從文件讀取參數(shù) 只需配置model掃描目錄 和dsName
public void configPlugin(PluginLoader pluginLoader) {
//第一個(gè)數(shù)據(jù)庫
ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(new DruidDataSourceProvider("default"), true);
activeRecordPlugin.addIncludePaths("cn.dreampie.resource");
pluginLoader.add(activeRecordPlugin);
}獨(dú)有優(yōu)點(diǎn)
1.極簡(jiǎn)的route設(shè)計(jì):
@GET("/users/:name")
//在路徑中自定義解析的參數(shù) 如果有其他符合 也可以用 /users/{name}
// 參數(shù)名就是方法變量名 除路徑參數(shù)之外的參數(shù)也可以放在方法參數(shù)里 傳遞方式 user={json字符串}
public Map find(String name,User user) {
// return Lister.of(name);
return Maper.of("k1", "v1,name:" + name, "k2", "v2");
//返回什么數(shù)據(jù)直接return,完全融入普通方法的方式
}2.極簡(jiǎn)的activerecord設(shè)計(jì),數(shù)據(jù)操作只需短短的一行 ,支持批量保存對(duì)象
//批量保存
User u1 = new User().set("username", "test").set("providername", "test").set("password", "123456");
User u2 = new User().set("username", "test").set("providername", "test").set("password", "123456");
User.dao.save(u1,u2);
//普通保存
User u = new User().set("username", "test").set("providername", "test").set("password", "123456");
u.save();
//更新
u.update();
//條件更新
User.dao.updateBy(columns,where,paras);
User.dao.updateAll(columns,paras);
//刪除
u.deleted();
//條件刪除
User.dao.deleteBy(where,paras);
User.dao.deleteAll();
//查詢
User.dao.findById(id);
User.dao.findBy(where,paras);
User.dao.findAll();
//分頁
User.dao.paginateBy(pageNumber,pageSize,where,paras);
User.dao.paginateAll(pageNumber,pageSize);3.極簡(jiǎn)的客戶端設(shè)計(jì),支持各種請(qǐng)求,文件上傳和文件下載(支持?jǐn)帱c(diǎn)續(xù)傳)
Client client=null;//創(chuàng)建客戶端對(duì)象
//啟動(dòng)resty-example項(xiàng)目,即可測(cè)試客戶端
String apiUrl = "http://localhost:8081/api/v1.0";
//如果不需要 使用賬號(hào)登陸
//client = new Client(apiUrl);
//如果有賬號(hào)權(quán)限限制 需要登陸
client = new Client(apiUrl, "/tests/login", "u", "123");
//該請(qǐng)求必須 登陸之后才能訪問 未登錄時(shí)返回 401 未認(rèn)證
ClientRequest authRequest = new ClientRequest("/users", HttpMethod.GET);
ResponseData authResult = client.build(authRequest).ask();
System.out.println(authResult.getData());
//get
ClientRequest getRequest = new ClientRequest("/tests", HttpMethod.GET);
ResponseData getResult = client.build(getRequest).ask();
System.out.println(getResult.getData());
//post
ClientRequest postRequest = new ClientRequest("/tests", HttpMethod.POST);
postRequest.addParameter("test", Jsoner.toJSONString(Maper.of("a", "諤諤")));
ResponseData postResult = client.build(postRequest).ask();
System.out.println(postResult.getData());
//put
ClientRequest putRequest = new ClientRequest("/tests/x", HttpMethod.PUT);
ResponseData putResult = client.build(putRequest).ask();
System.out.println(putResult.getData());
//delete
ClientRequest deleteRequest = new ClientRequest("/tests/a", HttpMethod.DELETE);
ResponseData deleteResult = client.build(deleteRequest).ask();
System.out.println(deleteResult.getData());
//upload
ClientRequest uploadRequest = new ClientRequest("/tests/resty", HttpMethod.POST);
uploadRequest.addUploadFiles("resty", ClientTest.class.getResource("/resty.jar").getFile());
uploadRequest.addParameter("des", "test file paras 測(cè)試筆");
ResponseData uploadResult = client.build(uploadRequest).ask();
System.out.println(uploadResult.getData());
//download 支持?jǐn)帱c(diǎn)續(xù)傳
ClientRequest downloadRequest = new ClientRequest("/tests/file", HttpMethod.GET);
downloadRequest.setDownloadFile(ClientTest.class.getResource("/resty.jar").getFile().replace(".jar", "x.jar"));
ResponseData downloadResult = client.build(downloadRequest).ask();
System.out.println(downloadResult.getData());4.支持多數(shù)據(jù)源和嵌套事務(wù)(使用場(chǎng)景:需要訪問多個(gè)數(shù)據(jù)庫的應(yīng)用,或者作為公司內(nèi)部的數(shù)據(jù)中間件向客戶端提供數(shù)據(jù)訪問api等)
// 在resource里使用事務(wù),也就是controller里,rest的世界認(rèn)為所以的請(qǐng)求都表示資源,所以這兒叫resource
@GET("/users")
@Transaction(name = {DS.DEFAULT_DS_NAME, "demo"}) //多數(shù)據(jù)源的事務(wù),如果你只有一個(gè)數(shù)據(jù)庫 直接@Transaction 不需要參數(shù)
public User transaction() {
//TODO 用model執(zhí)行數(shù)據(jù)庫的操作 只要有操作拋出異常 兩個(gè)數(shù)據(jù)源 都會(huì)回滾 雖然不是分布式事務(wù) 也能保證代碼塊的數(shù)據(jù)執(zhí)行安全
}
// 如果你需要在service里實(shí)現(xiàn)事務(wù),通過java動(dòng)態(tài)代理(必須使用接口,jdk設(shè)計(jì)就是這樣)
public interface UserService {
@Transaction(name = {DS.DEFAULT_DS_NAME, "demo"})//service里添加多數(shù)據(jù)源的事務(wù),如果你只有一個(gè)數(shù)據(jù)庫 直接@Transaction 不需要參數(shù)
public User save(User u);
}
// 在resource里使用service層的 事務(wù)
// @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"})的注解需要寫在service的接口上
// 注意java的自動(dòng)代理必須存在接口
// TransactionAspect 是事務(wù)切面 ,你也可以實(shí)現(xiàn)自己的切面比如日志的Aspect,實(shí)現(xiàn)Aspect接口
// 再private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect(),new LogAspect());
private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect());5.極簡(jiǎn)的權(quán)限設(shè)計(jì),你只需要實(shí)現(xiàn)一個(gè)簡(jiǎn)單接口和添加一個(gè)攔截器,即可實(shí)現(xiàn)基于url的權(quán)限設(shè)計(jì)
public void configInterceptor(InterceptorLoader interceptorLoader) { //權(quán)限攔截器 放在第一位 第一時(shí)間判斷 避免執(zhí)行不必要的代碼
interceptorLoader.add(new SecurityInterceptor(new MyAuthenticateService()));
}
//實(shí)現(xiàn)接口
public class MyAuthenticateService implements AuthenticateService {
//登陸時(shí) 通過name獲取用戶的密碼和權(quán)限信息
public Principal findByName(String name) {
DefaultPasswordService defaultPasswordService = new DefaultPasswordService();
Principal principal = new Principal(name, defaultPasswordService.hash("123"), new HashSet<String>() {{
add("api");
}});
return principal;
}
//基礎(chǔ)的權(quán)限總表 所以的url權(quán)限都放在這兒 你可以通過 文件或者數(shù)據(jù)庫或者直接代碼 來設(shè)置所有權(quán)限
public Set<Credential> loadAllCredentials() {
Set<Credential> credentials = new HashSet<Credential>();
credentials.add(new Credential("GET", "/api/v1.0/users**", "users"));
return credentials;
}
}6.極簡(jiǎn)的緩存設(shè)計(jì),可擴(kuò)展,非常簡(jiǎn)單即可啟用model的自動(dòng)緩存功能
public void configConstant(ConstantLoader constantLoader) {
//啟用緩存并在要自動(dòng)使用緩存的model上 開啟緩存@Table(name = "sec_user", cached = true)
constantLoader.setCacheEnable(true);
}
@Table(name = "sec_user", cached = true)
public class User extends Model<User> {
public static User dao = new User();
}7.下載文件,只需要直接return file
@GET("/files")
public File file() {
return new File(path);
}8.上傳文件,通過@FILE注解件寫到服務(wù)器
@POST("/files")
@FILE(dir = "/upload/") //配置上傳文件的相關(guān)信息
public UploadedFile file(UploadedFile file) {
return file;
}9.當(dāng)然也是支持傳統(tǒng)的web開發(fā),你可以自己實(shí)現(xiàn)數(shù)據(jù)解析,在config里添加自定義的解析模板
public void configConstant(ConstantLoader constantLoader) {
// 通過后綴來返回不同的數(shù)據(jù)類型 你可以自定義自己的 render 如:FreemarkerRender
// constantLoader.addRender("json", new JsonRender());
//默認(rèn)已添加json和text的支持,只需要把自定義的Render add即可
}二、運(yùn)行example示例:
1.運(yùn)行根目錄下的pom.xml->install (把相關(guān)的插件安裝到本地,功能完善之后發(fā)布到maven就不需要這樣了)
2.在本地mysql數(shù)據(jù)庫里創(chuàng)建demo,example數(shù)據(jù)庫,對(duì)應(yīng)application.properties的數(shù)據(jù)庫配置
3.運(yùn)行resty-example下的pom.xml->flyway-maven-plugin:migration,自動(dòng)根具resources下db目錄下的數(shù)據(jù)庫文件生成數(shù)據(jù)庫表結(jié)構(gòu)
4.運(yùn)行resty-example下的pom.xml->tomcat7-maven-plugin:run,啟動(dòng)example程序
提醒:推薦idea作為開發(fā)ide,使用分模塊的多module開發(fā)
以上就是極簡(jiǎn)的Resty服務(wù)端和客戶端RESTful框架的詳細(xì)內(nèi)容,更多關(guān)于Resty服務(wù)端客戶端RESTful框架的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
jsch中ChannelShell與ChannelExec的區(qū)別及說明
這篇文章主要介紹了jsch中ChannelShell與ChannelExec的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot?@DS注解實(shí)現(xiàn)多數(shù)據(jù)源配置以及問題解決辦法
這篇文章主要給大家介紹了關(guān)于SpringBoot?@DS注解實(shí)現(xiàn)多數(shù)據(jù)源配置以及問題解決辦法,所謂多數(shù)據(jù)源就是一個(gè)Java EE項(xiàng)目中采用了不同數(shù)據(jù)庫實(shí)例中的多個(gè)庫,或者是同一個(gè)數(shù)據(jù)庫實(shí)例中的多個(gè)不同庫,需要的朋友可以參考下2023-11-11
Spring boot攔截器實(shí)現(xiàn)IP黑名單的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring boot攔截器實(shí)現(xiàn)IP黑名單的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring boot攔截器具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
詳解SpringBoot與SpringCloud的版本對(duì)應(yīng)詳細(xì)版
這篇文章主要介紹了詳解SpringBoot與SpringCloud的版本對(duì)應(yīng)詳細(xì)版,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
MyBatis-Plus中最簡(jiǎn)單的查詢操作教程(Lambda)
這篇文章主要給大家介紹了關(guān)于MyBatis-Plus中最簡(jiǎn)單的查詢操作的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03

