1. Cordra服务端下载使用(windows系统下)

  1. 下载地址:cordra-2.3.0
  2. 部署Cordra时有很多选项可配置。最简单的是一个独立的Cordra服务,它使用本地文件系统存储bdbje和一个嵌入式索引器lucene。同时也是默认的模式配置,默认模式方便用来测试Cordra功能或使用Cordra构建任何应用程序。
  3. 解压下载的zip文件,进入cordra-2.3.0\data路径下,修改repoInit.json初始配置文件,设置初始的密码和标识句柄前缀,从该前缀将生成唯一标识符;如果不设置前缀,则默认情况下将使用测试用前缀来创建对象。

    1. {
    2. "adminPassword": "admin123",
    3. "prefix": "86.1234.abc.TEST"
    4. }
  4. 回退到cordra-2.3.0路径下,点击startup.bat启动服务,可以看到服务的访问路径http://127.0.0.1:8080/和https协议的访问路径https://127.0.0.1:8443

1626142570565_5B3E2155-BF3C-402a-B640-6F5E590F0602.png

  1. 写一个java测试项目,springboot框架工具下导入pom依赖。因cordra包内的httpclient需要较高版本,最好带上对应的httpclient依赖,避免和spring-boot-starter-web依赖起冲突;gson依赖同理

    1. <dependency>
    2. <groupId>net.cnri.cordra</groupId>
    3. <artifactId>cordra-client</artifactId>
    4. <version>2.2.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.apache.httpcomponents</groupId>
    8. <artifactId>httpclient</artifactId>
    9. <version>4.5.12</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>com.google.code.gson</groupId>
    13. <artifactId>gson</artifactId>
    14. <version>2.8.6</version>
    15. </dependency>
  2. application.properties配置启动端口号,因为8080被cordra服务占用,改用其它端口号,如8081;另外因为使用了gson,所以需要配置对应的spring转换配置

    1. server.port=8081
    2. spring.http.converters.preferred-json-mapper=gson
    3. #Spring Boot >= 2.3.0.RELEASE情况下用下面配置
    4. #spring.mvc.converters.preferred-json-mapper=gson
  3. 写一段Restful风格测试代码,CRUD数字对象 ```java package com.dtdream.test.cordra;

import net.cnri.cordra.api.CordraClient; import net.cnri.cordra.api.CordraException; import net.cnri.cordra.api.CordraObject; import net.cnri.cordra.api.SearchResults; import net.cnri.cordra.api.TokenUsingHttpCordraClient; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;

import java.util.List; import java.util.stream.Collectors;

@RestController public class CordraController {

  1. static CordraClient cordra;
  2. static {
  3. try {
  4. cordra = new TokenUsingHttpCordraClient("https://127.0.0.1:8443", "admin", "admin123");
  5. } catch (CordraException e) {
  6. e.printStackTrace();
  7. }
  8. }
  9. /**
  10. * 新增
  11. *
  12. * @param
  13. * @return
  14. */
  15. @PostMapping
  16. public CordraObject create(@RequestBody CordraObject cordraObject) throws CordraException {
  17. return cordra.create(cordraObject);
  18. }
  19. /**
  20. * 获取单个对象
  21. *
  22. * @param
  23. * @return
  24. */
  25. @GetMapping
  26. public CordraObject get(@RequestParam String id) throws CordraException {
  27. return cordra.get(id);
  28. }
  29. /**
  30. * 获取所有对象
  31. *
  32. * @param
  33. * @return
  34. */
  35. @GetMapping("all")
  36. public List<CordraObject> getAll() throws CordraException {
  37. SearchResults<CordraObject> results = cordra.search("*:*");
  38. return results.stream().collect(Collectors.toList());
  39. }
  40. /**
  41. * 更新单个对象
  42. *
  43. * @param
  44. * @return
  45. */
  46. @PutMapping
  47. public CordraObject update(@RequestBody CordraObject cordraObject) throws CordraException {
  48. return cordra.update(cordraObject);
  49. }
  50. /**
  51. * 删除单个对象
  52. *
  53. * @param
  54. * @return
  55. */
  56. @DeleteMapping
  57. public CordraObject delete(@RequestParam String id) throws CordraException {
  58. cordra.delete(id);
  59. return cordra.get(id);
  60. }

}

  1. 8. postman访问接口进行结果验证
  2. #新建对象<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22093066/1626156148262-e29082d7-b68d-4d1e-ae26-a7db03d6b48e.png#clientId=u07e33d78-6b25-4&from=paste&height=706&id=uffce7a28&margin=%5Bobject%20Object%5D&name=image.png&originHeight=706&originWidth=610&originalType=binary&ratio=1&size=47752&status=done&style=none&taskId=u8ff0a6be-8441-49b3-bcc5-9a48d02de1b&width=610)<br />#获取单个对象<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22093066/1626156207591-dddc8c65-9326-4f58-809e-c11042aadb8d.png#clientId=u07e33d78-6b25-4&from=paste&height=693&id=u097246bb&margin=%5Bobject%20Object%5D&name=image.png&originHeight=693&originWidth=607&originalType=binary&ratio=1&size=42681&status=done&style=none&taskId=u25ba3dc9-827d-40c3-a27a-4a56218ecef&width=607)<br />#更新对象<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22093066/1626156284115-a1db6fa6-b060-45dd-8e6e-fb897eaf059b.png#clientId=u07e33d78-6b25-4&from=paste&height=681&id=u8b8ae7db&margin=%5Bobject%20Object%5D&name=image.png&originHeight=681&originWidth=613&originalType=binary&ratio=1&size=46592&status=done&style=none&taskId=uae6b3bda-b60b-4570-a3e9-9fd9cc44e74&width=613)<br />#删除对象<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22093066/1626156340161-42532c81-5b40-4631-ad34-81da6eb5ba9e.png#clientId=u07e33d78-6b25-4&from=paste&height=448&id=u61cdc279&margin=%5Bobject%20Object%5D&name=image.png&originHeight=448&originWidth=610&originalType=binary&ratio=1&size=26515&status=done&style=none&taskId=u1f4ce95a-e0e5-49cd-91b6-d6701fb4c23&width=610)<br />#获取所有对象http://localhost:8081/all,结果包含几个建立的系统数据对象,【id=test/123】的是另一个测试数据
  3. ```json
  4. [
  5. {
  6. "id": "86.1234.abc.TEST/0cbd3f8ad53a8f1d8209",
  7. "type": "Schema",
  8. "content": {
  9. "identifier": "86.1234.abc.TEST/0cbd3f8ad53a8f1d8209",
  10. "name": "Document",
  11. "schema": {
  12. "type": "object",
  13. "title": "Document",
  14. "required": [
  15. "id"
  16. ],
  17. "properties": {
  18. "id": {
  19. "type": "string",
  20. "cordra": {
  21. "type": {
  22. "autoGeneratedField": "handle"
  23. }
  24. }
  25. },
  26. "name": {
  27. "type": "string",
  28. "title": "Name",
  29. "cordra": {
  30. "preview": {
  31. "showInPreview": true,
  32. "isPrimary": true
  33. }
  34. }
  35. },
  36. "description": {
  37. "type": "string",
  38. "format": "textarea",
  39. "title": "Description",
  40. "cordra": {
  41. "preview": {
  42. "showInPreview": true
  43. }
  44. }
  45. },
  46. "creationDate": {
  47. "type": "string",
  48. "title": "Creation Date",
  49. "cordra": {
  50. "type": {
  51. "autoGeneratedField": "creationDate"
  52. }
  53. }
  54. },
  55. "modificationDate": {
  56. "type": "string",
  57. "title": "Modification Date",
  58. "cordra": {
  59. "preview": {
  60. "showInPreview": true
  61. },
  62. "type": {
  63. "autoGeneratedField": "modificationDate"
  64. }
  65. }
  66. },
  67. "createdBy": {
  68. "type": "string",
  69. "title": "Created By",
  70. "cordra": {
  71. "type": {
  72. "autoGeneratedField": "createdBy"
  73. }
  74. }
  75. },
  76. "modifiedBy": {
  77. "type": "string",
  78. "title": "Modified By",
  79. "cordra": {
  80. "type": {
  81. "autoGeneratedField": "modifiedBy"
  82. }
  83. }
  84. }
  85. }
  86. }
  87. },
  88. "metadata": {
  89. "createdOn": 1626142554573,
  90. "createdBy": "admin",
  91. "modifiedOn": 1626142554573,
  92. "modifiedBy": "admin",
  93. "txnId": 1626142554541005
  94. }
  95. },
  96. {
  97. "id": "86.1234.abc.TEST/fe13890f0aed2e634da6",
  98. "type": "Schema",
  99. "content": {
  100. "identifier": "86.1234.abc.TEST/fe13890f0aed2e634da6",
  101. "name": "User",
  102. "schema": {
  103. "type": "object",
  104. "required": [
  105. "id",
  106. "username",
  107. "password"
  108. ],
  109. "properties": {
  110. "id": {
  111. "type": "string",
  112. "cordra": {
  113. "type": {
  114. "autoGeneratedField": "handle"
  115. }
  116. }
  117. },
  118. "username": {
  119. "type": "string",
  120. "title": "Username",
  121. "cordra": {
  122. "preview": {
  123. "showInPreview": true,
  124. "isPrimary": true
  125. },
  126. "auth": "username"
  127. }
  128. },
  129. "password": {
  130. "type": "string",
  131. "format": "password",
  132. "title": "Password",
  133. "cordra": {
  134. "auth": "password"
  135. }
  136. },
  137. "requirePasswordChange": {
  138. "type": "boolean",
  139. "title": "Require Password Change",
  140. "description": "If true a new password must be set on next authentication.",
  141. "cordra": {
  142. "auth": "requirePasswordChange"
  143. }
  144. }
  145. }
  146. },
  147. "javascript": "exports.beforeSchemaValidation = beforeSchemaValidation;\n\nfunction beforeSchemaValidation(obj, context) {\n if (!context.useLegacyContentOnlyJavaScriptHooks) {\n obj.content = beforeSchemaValidationLegacy(obj.content, context);\n return obj;\n } else {\n return beforeSchemaValidationLegacy(obj, context);\n }\n}\n\nfunction beforeSchemaValidationLegacy(obj, context) {\n if (!obj.id) obj.id = \"\";\n if (!obj.password) obj.password = \"\";\n var password = obj.password;\n if (context.isNew || password) {\n if (password.length < 8) {\n throw \"Password is too short. Min length 8 characters\";\n }\n }\n return obj;\n}\n"
  148. },
  149. "metadata": {
  150. "createdOn": 1626142554295,
  151. "createdBy": "admin",
  152. "modifiedOn": 1626142554295,
  153. "modifiedBy": "admin",
  154. "txnId": 1626142554262003
  155. }
  156. },
  157. {
  158. "id": "86.1234.abc.TEST/fec9da465d2c680caf42",
  159. "type": "Schema",
  160. "content": {
  161. "identifier": "86.1234.abc.TEST/fec9da465d2c680caf42",
  162. "name": "Group",
  163. "schema": {
  164. "type": "object",
  165. "required": [
  166. "id",
  167. "groupName",
  168. "description"
  169. ],
  170. "properties": {
  171. "id": {
  172. "type": "string",
  173. "cordra": {
  174. "type": {
  175. "autoGeneratedField": "handle"
  176. }
  177. }
  178. },
  179. "groupName": {
  180. "type": "string",
  181. "title": "Group Name",
  182. "cordra": {
  183. "preview": {
  184. "showInPreview": true,
  185. "isPrimary": true
  186. }
  187. }
  188. },
  189. "description": {
  190. "type": "string",
  191. "format": "textarea",
  192. "maxLength": 2048,
  193. "title": "Description",
  194. "cordra": {
  195. "preview": {
  196. "showInPreview": true,
  197. "excludeTitle": true
  198. }
  199. }
  200. },
  201. "users": {
  202. "type": "array",
  203. "format": "table",
  204. "title": "Users",
  205. "uniqueItems": true,
  206. "items": {
  207. "type": "string",
  208. "title": "User",
  209. "cordra": {
  210. "type": {
  211. "handleReference": {
  212. "types": [
  213. "User",
  214. "Group"
  215. ]
  216. }
  217. }
  218. }
  219. },
  220. "cordra": {
  221. "auth": "usersList"
  222. }
  223. }
  224. }
  225. }
  226. },
  227. "metadata": {
  228. "createdOn": 1626142554462,
  229. "createdBy": "admin",
  230. "modifiedOn": 1626142554462,
  231. "modifiedBy": "admin",
  232. "txnId": 1626142554429004
  233. }
  234. },
  235. {
  236. "id": "test/123",
  237. "type": "Document",
  238. "content": {
  239. "id": "test/123",
  240. "name": "first example name",
  241. "description": "The first test data"
  242. },
  243. "metadata": {
  244. "createdOn": 1626154301602,
  245. "createdBy": "admin",
  246. "modifiedOn": 1626154301602,
  247. "modifiedBy": "admin",
  248. "txnId": 1626154301607000
  249. }
  250. }
  251. ]