项目作者: mattkol

项目描述 :
SugarOnRest is a Java SugarCRM/SuiteCRM Rest API Client. It is Java Wrapper for the SugarCRM/SuiteCRM REST API Client.
高级语言: Java
项目地址: git://github.com/mattkol/SugarOnRest.git
创建时间: 2017-01-14T02:57:59Z
项目社区:https://github.com/mattkol/SugarOnRest

开源协议:MIT License

下载


Being RESTful with SugarCRM/SuiteCRM in Java

SugarOnRest is a Java SugarCRM CE 6.x/SuiteCRM 7.x API client. SugarOnRest is a UniRest Java implementation. It is a Restful CRUD client that implements the SugarCRM module Create, Read, Update and Delete functionalities.

SugarOnRest implements following SugarCRM REST API method calls: oauth_access, get_entry, get_entry_list, set_entry, set_entries.

This is a port of .NET C# SugarRestSharp.

For more info/documentation, please check SugarOnRest wiki

Basic Sample Usages

  1. String sugarCrmUrl = "http://demo.suiteondemand.com/service/v4_1/rest.php";
  2. String sugarCrmUsername = "will";
  3. String sugarCrmPassword = "will";
  4. SugarRestClient client = new SugarRestClient(sugarCrmUrl, sugarCrmUsername, sugarCrmPassword);
  5. // Option 1 - Read by known Java Pojo type - Accounts.
  6. SugarRestRequest accountRequest = new SugarRestRequest(Accounts.class, RequestType.ReadById);
  7. // set the account id to read.
  8. accountRequest.setParameter("2da8333f-10b8-d173-e38c-587662482d83");
  9. SugarRestResponse accountResponse = client.execute(accountRequest);
  10. Accounts account = (Accounts)accountResponse.getData();
  11. // Option 2 - Read by known SugarCRM module name - "Contacts".
  12. SugarRestRequest contactRequest = new SugarRestRequest("Contacts", RequestType.ReadById);
  13. contactRequest.setParameter("1b680648-20ca-cd20-692e-584fbec623e5");
  14. SugarRestResponse contactRresponse = client.execute(contactRequest);
  15. Contacts contact = (Contacts)contactRresponse.getData();

Advanced Sample Usage - Linked Module

This sample usage shows how to read “Accounts” module entity data with linked modules (link “Contacts” module). For more request options make changes to the
Request Options

This implements the get_entry SugarCRM REST API method setting the link_name_to_fields_array parameter.

  1. package com.sugaronrest.tests;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.sugaronrest.*;
  4. import com.sugaronrest.utils.JsonObjectMapper;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. String url = "http://demo.suiteondemand.com/service/v4_1/rest.php";
  11. String username = "will";
  12. String password = "will";
  13. SugarRestClient client = new SugarRestClient(url, username, password);
  14. String accountId = "2da8333f-10b8-d173-e38c-587662482d83";
  15. SugarRestRequest request = new SugarRestRequest(RequestType.LinkedReadById);
  16. request.setModuleName("Accounts");
  17. request.setParameter(accountId);
  18. List<String> selectedFields = new ArrayList<String>();
  19. selectedFields.add(NameOf.Accounts.Id);
  20. selectedFields.add(NameOf.Accounts.Name);
  21. selectedFields.add(NameOf.Accounts.Industry);
  22. selectedFields.add(NameOf.Accounts.Website);
  23. selectedFields.add(NameOf.Accounts.ShippingAddressCity);
  24. request.getOptions().setSelectFields(selectedFields);
  25. Map<Object, List<String>> linkedListInfo = new HashMap<Object, List<String>>();
  26. List<String> selectContactFields = new ArrayList<String>();
  27. selectContactFields.add(NameOf.Contacts.FirstName);
  28. selectContactFields.add(NameOf.Contacts.LastName);
  29. selectContactFields.add(NameOf.Contacts.Title);
  30. selectContactFields.add(NameOf.Contacts.Description);
  31. selectContactFields.add(NameOf.Contacts.PrimaryAddressPostalcode);
  32. linkedListInfo.put(Contacts.class, selectContactFields);
  33. request.getOptions().setLinkedModules(linkedListInfo);
  34. SugarRestResponse response = client.execute(request);

Custom model

  1. package com.sugaronrest.tests.custommodels;
  2. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  3. import com.fasterxml.jackson.annotation.JsonProperty;
  4. import com.sugaronrest.modules.Accounts;
  5. import com.sugaronrest.modules.Contacts;
  6. import java.util.List;
  7. @JsonIgnoreProperties(ignoreUnknown = true)
  8. public class CustomAcccount1 extends Accounts {
  9. public List<Contacts> getContactLink() {
  10. return contactLink;
  11. }
  12. public void setContactLink(List<Contacts> value) {
  13. contactLink = value;
  14. }
  15. @JsonProperty("contacts")
  16. private List<Contacts> contactLink;
  17. }

Response (getData())

  1. getData() = null;
  2. // Deserialize json data to custom object
  3. ObjectMapper mapper = JsonObjectMapper.getMapper();
  4. CustomAcccount1 customAccount = mapper.readValue(jsonData, CustomAcccount1.class);

Response (getJData())

  1. {
  2. "website": "www.veganthe.co.jp",
  3. "name": "RR. Talker Co",
  4. "industry": "Media",
  5. "id": "2da8333f-10b8-d173-e38c-587662482d83",
  6. "shipping_address_city": "St. Petersburg",
  7. "contacts": [
  8. {
  9. "primary_address_postalcode": "24688",
  10. "last_name": "Bhatt",
  11. "description": "",
  12. "title": "IT Developer",
  13. "first_name": "Corina"
  14. },
  15. {
  16. "primary_address_postalcode": "39916",
  17. "last_name": "Lehner",
  18. "description": "",
  19. "title": "Senior Product Manager",
  20. "first_name": "Ramiro"
  21. },
  22. {
  23. "primary_address_postalcode": "38602",
  24. "last_name": "Delorenzo",
  25. "description": "",
  26. "title": "Senior Product Manager",
  27. "first_name": "Kelley"
  28. },
  29. {
  30. "primary_address_postalcode": "81029",
  31. "last_name": "Paulin",
  32. "description": "",
  33. "title": "VP Operations",
  34. "first_name": "Sheree"
  35. },
  36. {
  37. "primary_address_postalcode": "91550",
  38. "last_name": "Clopton",
  39. "description": "",
  40. "title": "IT Developer",
  41. "first_name": "Jacqueline"
  42. },
  43. {
  44. "primary_address_postalcode": "19042",
  45. "last_name": "Hong",
  46. "description": "",
  47. "title": "IT Developer",
  48. "first_name": "Juanita"
  49. },
  50. {
  51. "primary_address_postalcode": "29452",
  52. "last_name": "Pagano",
  53. "description": "",
  54. "title": "IT Developer",
  55. "first_name": "Lamar"
  56. },
  57. {
  58. "primary_address_postalcode": "88939",
  59. "last_name": "Cossey",
  60. "description": "",
  61. "title": "President",
  62. "first_name": "Bernadine"
  63. },
  64. {
  65. "primary_address_postalcode": "40382",
  66. "last_name": "Bast",
  67. "description": "",
  68. "title": "Director Sales",
  69. "first_name": "Paul"
  70. }
  71. ]
  72. }

Response (getJsonRawRequest())

  1. {
  2. "method": "get_entry",
  3. "input_type": "json",
  4. "response_type": "json",
  5. "rest_data": {
  6. "session": "2p7qi9qpvjvic3et2t5m410e31",
  7. "module_name": "Accounts",
  8. "id": "2da8333f-10b8-d173-e38c-587662482d83",
  9. "select_fields": [
  10. "id",
  11. "name",
  12. "industry",
  13. "website",
  14. "shipping_address_city"
  15. ],
  16. "link_name_to_fields_array": [
  17. {
  18. "name": "contacts",
  19. "value": [
  20. "first_name",
  21. "last_name",
  22. "title",
  23. "description",
  24. "primary_address_postalcode"
  25. ]
  26. }
  27. ],
  28. "track_view": false
  29. }
  30. }

Response (getJsonRawResponse())

  1. {
  2. "entry_list": [
  3. {
  4. "id": "2da8333f-10b8-d173-e38c-587662482d83",
  5. "module_name": "Accounts",
  6. "name_value_list": {
  7. "id": {
  8. "name": "id",
  9. "value": "2da8333f-10b8-d173-e38c-587662482d83"
  10. },
  11. "name": {
  12. "name": "name",
  13. "value": "RR. Talker Co"
  14. },
  15. "industry": {
  16. "name": "industry",
  17. "value": "Media"
  18. },
  19. "website": {
  20. "name": "website",
  21. "value": "www.veganthe.co.jp"
  22. },
  23. "shipping_address_city": {
  24. "name": "shipping_address_city",
  25. "value": "St. Petersburg"
  26. }
  27. }
  28. }
  29. ],
  30. "relationship_list": [
  31. [
  32. {
  33. "name": "contacts",
  34. "records": [
  35. {
  36. "first_name": {
  37. "name": "first_name",
  38. "value": "Corina"
  39. },
  40. "last_name": {
  41. "name": "last_name",
  42. "value": "Bhatt"
  43. },
  44. "title": {
  45. "name": "title",
  46. "value": "IT Developer"
  47. },
  48. "description": {
  49. "name": "description",
  50. "value": ""
  51. },
  52. "primary_address_postalcode": {
  53. "name": "primary_address_postalcode",
  54. "value": "24688"
  55. }
  56. },
  57. {
  58. "first_name": {
  59. "name": "first_name",
  60. "value": "Ramiro"
  61. },
  62. "last_name": {
  63. "name": "last_name",
  64. "value": "Lehner"
  65. },
  66. "title": {
  67. "name": "title",
  68. "value": "Senior Product Manager"
  69. },
  70. "description": {
  71. "name": "description",
  72. "value": ""
  73. },
  74. "primary_address_postalcode": {
  75. "name": "primary_address_postalcode",
  76. "value": "39916"
  77. }
  78. },
  79. {
  80. "first_name": {
  81. "name": "first_name",
  82. "value": "Kelley"
  83. },
  84. "last_name": {
  85. "name": "last_name",
  86. "value": "Delorenzo"
  87. },
  88. "title": {
  89. "name": "title",
  90. "value": "Senior Product Manager"
  91. },
  92. "description": {
  93. "name": "description",
  94. "value": ""
  95. },
  96. "primary_address_postalcode": {
  97. "name": "primary_address_postalcode",
  98. "value": "38602"
  99. }
  100. },
  101. {
  102. "first_name": {
  103. "name": "first_name",
  104. "value": "Sheree"
  105. },
  106. "last_name": {
  107. "name": "last_name",
  108. "value": "Paulin"
  109. },
  110. "title": {
  111. "name": "title",
  112. "value": "VP Operations"
  113. },
  114. "description": {
  115. "name": "description",
  116. "value": ""
  117. },
  118. "primary_address_postalcode": {
  119. "name": "primary_address_postalcode",
  120. "value": "81029"
  121. }
  122. },
  123. {
  124. "first_name": {
  125. "name": "first_name",
  126. "value": "Jacqueline"
  127. },
  128. "last_name": {
  129. "name": "last_name",
  130. "value": "Clopton"
  131. },
  132. "title": {
  133. "name": "title",
  134. "value": "IT Developer"
  135. },
  136. "description": {
  137. "name": "description",
  138. "value": ""
  139. },
  140. "primary_address_postalcode": {
  141. "name": "primary_address_postalcode",
  142. "value": "91550"
  143. }
  144. },
  145. {
  146. "first_name": {
  147. "name": "first_name",
  148. "value": "Juanita"
  149. },
  150. "last_name": {
  151. "name": "last_name",
  152. "value": "Hong"
  153. },
  154. "title": {
  155. "name": "title",
  156. "value": "IT Developer"
  157. },
  158. "description": {
  159. "name": "description",
  160. "value": ""
  161. },
  162. "primary_address_postalcode": {
  163. "name": "primary_address_postalcode",
  164. "value": "19042"
  165. }
  166. },
  167. {
  168. "first_name": {
  169. "name": "first_name",
  170. "value": "Lamar"
  171. },
  172. "last_name": {
  173. "name": "last_name",
  174. "value": "Pagano"
  175. },
  176. "title": {
  177. "name": "title",
  178. "value": "IT Developer"
  179. },
  180. "description": {
  181. "name": "description",
  182. "value": ""
  183. },
  184. "primary_address_postalcode": {
  185. "name": "primary_address_postalcode",
  186. "value": "29452"
  187. }
  188. },
  189. {
  190. "first_name": {
  191. "name": "first_name",
  192. "value": "Bernadine"
  193. },
  194. "last_name": {
  195. "name": "last_name",
  196. "value": "Cossey"
  197. },
  198. "title": {
  199. "name": "title",
  200. "value": "President"
  201. },
  202. "description": {
  203. "name": "description",
  204. "value": ""
  205. },
  206. "primary_address_postalcode": {
  207. "name": "primary_address_postalcode",
  208. "value": "88939"
  209. }
  210. },
  211. {
  212. "first_name": {
  213. "name": "first_name",
  214. "value": "Paul"
  215. },
  216. "last_name": {
  217. "name": "last_name",
  218. "value": "Bast"
  219. },
  220. "title": {
  221. "name": "title",
  222. "value": "Director Sales"
  223. },
  224. "description": {
  225. "name": "description",
  226. "value": ""
  227. },
  228. "primary_address_postalcode": {
  229. "name": "primary_address_postalcode",
  230. "value": "40382"
  231. }
  232. }
  233. ]
  234. }
  235. ]
  236. ]
  237. }