使用Java在Confluence中通过Rest API创建页面

有没有人有任何关于如何使用Confluence的RESTful API在Confluence中创建页面/ wiki条目的示例? 我正在尝试用Java编写可以做到这一点的东西。

先感谢您…

谢谢,我已经在线检查了文档但是在Confluence REST API中找不到任何使用JAVA的示例。 这就是我在这里发布的原因。

无论如何,我想我弄清楚了:

import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; /** * Creates a Confluence wiki page via the RESTul API * using an HTTP Post command. */ public class ConfluenceRestApi2CreateEntry { //private static final String BASE_URL = "http://localhost:1990/confluence"; private static final String BASE_URL = "https://.atlassian.net/wiki"; private static final String USERNAME = "username"; private static final String PASSWORD = "password"; private static final String ENCODING = "utf-8"; public static String createContentRestUrl()throws UnsupportedEncodingException { return String.format("%s/rest/api/content/?&os_authType=basic&os_username=%s&os_password=%s", BASE_URL, URLEncoder.encode(USERNAME, ENCODING), URLEncoder.encode(PASSWORD, ENCODING)); } public static void main(final String[] args) throws Exception { String wikiPageTitle = "My Awesome Page"; String wikiPage = "

Things That Are Awesome

  • Birds
  • Mammals
  • Decapods
"; String wikiSpace = "JOUR"; String labelToAdd = "awesome_stuff"; int parentPageId = 9994250; JSONObject newPage = defineConfluencePage(wikiPageTitle, wikiPage, wikiSpace, labelToAdd, parentPageId); createConfluencePageViaPost(newPage); } public static void createConfluencePageViaPost(JSONObject newPage) throws Exception { HttpClient client = new DefaultHttpClient(); // Send update request HttpEntity pageEntity = null; try { //2016-12-18 - StirlingCrow: Left off here. Was finally able to get the post command to work //I can begin testing adding more data to the value stuff (see above) HttpPost postPageRequest = new HttpPost(createContentRestUrl()); StringEntity entity = new StringEntity(newPage.toString(), ContentType.APPLICATION_JSON); postPageRequest.setEntity(entity); HttpResponse postPageResponse = client.execute(postPageRequest); pageEntity = postPageResponse.getEntity(); System.out.println("Push Page Request returned " + postPageResponse.getStatusLine().toString()); System.out.println(""); System.out.println(IOUtils.toString(pageEntity.getContent())); } finally { EntityUtils.consume(pageEntity); } } public static JSONObject defineConfluencePage(String pageTitle, String wikiEntryText, String pageSpace, String label, int parentPageId) throws JSONException { //This would be the command in Python (similar to the example //in the Confluence example: // //curl -u : -X POST -H 'Content-Type: application/json' -d'{ // "type":"page", // "title":"My Awesome Page", // "ancestors":[{"id":9994246}], // "space":{"key":"JOUR"}, // "body": // {"storage": // {"value":"

Things That Are Awesome

  • Birds
  • Mammals
  • Decapods
", // "representation":"storage"} // }, // "metadata": // {"labels":[ // {"prefix":"global", // "name":"journal"}, // {"prefix":"global", // "name":"awesome_stuff"} // ] // } // }' // http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool JSONObject newPage = new JSONObject(); // "type":"page", // "title":"My Awesome Page" newPage.put("type","page"); newPage.put("title", pageTitle); // "ancestors":[{"id":9994246}], JSONObject parentPage = new JSONObject(); parentPage.put("id",parentPageId); JSONArray parentPageArray = new JSONArray(); parentPageArray.put(parentPage); newPage.put("ancestors", parentPageArray); // "space":{"key":"JOUR"}, JSONObject spaceOb = new JSONObject(); spaceOb.put("key",pageSpace); newPage.put("space", spaceOb); // "body": // {"storage": // {"value":"

Things That Are Awesome

  • Birds
  • Mammals
  • Decapods

", // "representation":"storage"} // }, JSONObject jsonObjects = new JSONObject(); jsonObjects.put("value", wikiEntryText); jsonObjects.put("representation","storage"); JSONObject storageObject = new JSONObject(); storageObject.put("storage", jsonObjects); newPage.put("body", storageObject); //LABELS // "metadata": // {"labels":[ // {"prefix":"global", // "name":"journal"}, // {"prefix":"global", // "name":"awesome_stuff"} // ] // } JSONObject prefixJsonObject1 = new JSONObject(); prefixJsonObject1.put("prefix","global"); prefixJsonObject1.put("name","journal"); JSONObject prefixJsonObject2 = new JSONObject(); prefixJsonObject2.put("prefix","global"); prefixJsonObject2.put("name",label); JSONArray prefixArray = new JSONArray(); prefixArray.put(prefixJsonObject1); prefixArray.put(prefixJsonObject2); JSONObject labelsObject = new JSONObject(); labelsObject.put("labels", prefixArray); newPage.put("metadata",labelsObject); return newPage; } }

这是我在GitHub中创建的一个项目,其中还有一个使用Java简单地提取wiki页面条目的示例: