2014-10-31 33 views
2

我已經寫了一個Java程序(只使用主法)發送使用JSON HTML內容的表中插入數據。 我有我的對象在單獨的程序中的一些數據,我想以編程方式導入我的Atlassian Confluence頁面的 我仍然只能在頁面內插入靜態HTML內容和數據。 我從未與json合作過。 如何在表格的行中插入數據。如何在Atlassian的合流頁面使用Java

public class Example { 
 

 
\t private static final String BASE_URL = "www.example.com"; 
 
\t private static final String USERNAME = "xxxxx"; 
 
\t private static final String PASSWORD = "xxxxx"; 
 
\t private static final String ENCODING = "utf-8"; 
 
\t 
 
\t static HttpResponse putPageResponse; 
 
\t static HttpEntity putPageEntity = null; 
 

 
\t private static String getContentRestUrl(final Long contentId, 
 
\t \t \t final String[] expansions) throws UnsupportedEncodingException { 
 
\t \t \t final String expand = URLEncoder.encode(StringUtils.join(expansions, ","), ENCODING); 
 
\t \t return String 
 
\t \t \t \t .format("%s/rest/api/content/%s?expand=%s&os_authType=basic&os_username=%s&os_password=%s", 
 
\t \t \t \t \t \t BASE_URL, contentId, expand, 
 
\t \t \t \t \t \t URLEncoder.encode(USERNAME, ENCODING), 
 
\t \t \t \t \t \t URLEncoder.encode(PASSWORD, ENCODING)); 
 
\t } 
 

 
\t public static void main(final String[] args) throws Exception { 
 
\t \t final long pageId = 00000000; 
 
\t \t HttpClient client = new DefaultHttpClient(); 
 
\t \t // Get current page version 
 
\t \t String pageObj = null; 
 
\t \t HttpEntity pageEntity = null; 
 
\t \t try { 
 
\t \t \t HttpGet getPageRequest = new HttpGet(getContentRestUrl(pageId, 
 
\t \t \t \t \t new String[] { "body.storage", "version", "ancestors" })); 
 
\t \t \t 
 
\t \t \t HttpResponse getPageResponse = client.execute(getPageRequest); 
 
\t \t \t 
 
\t \t \t pageEntity = getPageResponse.getEntity(); 
 
\t \t \t pageObj = IOUtils.toString(pageEntity.getContent()); 
 
\t \t 
 
\t \t } finally { 
 
\t \t \t if (pageEntity != null) { 
 
\t \t \t \t EntityUtils.consume(pageEntity); 
 
\t \t \t } 
 
\t \t } 
 

 
\t \t // Parse response into JSON 
 
\t \t JSONObject page = new JSONObject(pageObj); 
 
\t \t try { 
 
\t 
 
\t \t **page.getJSONObject("body").getJSONObject("storage") 
 
\t \t .put("value","<b>html content here<b>");** 
 
\t \t <!--if i try to write any thing in second param of put() than it replace the all content of body with that.--> 
 
\t \t 
 
\t \t int currentVersion = page.getJSONObject("version").getInt("number"); 
 
\t \t page.getJSONObject("version").put("number", currentVersion + 1); 
 

 
\t \t // Send update request 
 
\t \t 
 
\t \t \t HttpPut putPageRequest = new HttpPut(getContentRestUrl(pageId, 
 
\t \t \t \t \t new String[] {})); 
 

 
\t \t \t StringEntity entity = new StringEntity(page.toString(), 
 
\t \t \t \t \t ContentType.APPLICATION_JSON); 
 
\t \t \t putPageRequest.setEntity(entity); 
 

 
\t \t \t putPageResponse = client.execute(putPageRequest); 
 
\t \t \t System.out.println(""); 
 
\t \t \t EntityUtils.consume(putPageEntity); 
 
\t \t 
 
\t \t \t 
 
\t \t } catch(Exception e) { 
 
\t \t \t System.out.println("exception is: "+e); 
 
\t \t } 
 
\t } 
 
}

** 如果任何一個份額,任何示例代碼,會在我非常有幫助。 我在我的頁面中有以下表格。

__________________________________________ |

| S.no |書名|書籍作者|發佈日期|價格
| _____ | ________ | ___________ | __________ | ______ |

+0

在這個網站上還有很多其他的問題處理所有這些主題 - 例如,我想你可以在這裏快速搜索「Java REST」,並找到很多答案。 – mskfisher 2014-10-31 12:54:22

+1

'REST - > JSON'聽起來很不錯。 – EpicPandaForce 2014-10-31 13:02:37

回答

2

您可以使用Confluence REST API執行此操作。從Confluence 5.5起,有一個新的REST API。完全是documented here

特別要考慮這個端點:

POST:/rest/api/content

的Content-Type:application/json

BODY:

{ 
    "type":"page", 
    "title":"My test page", 
    "space":{ 
     "key":"DEMO" 
    }, 
    "body":{ 
     "storage":{ 
     "value":"<p>This is a new page</p>", 
     "representation":"storage" 
     } 
    } 
} 

這裏,將增加一個例子頂部l的一個頁面空間的埃維爾:

curl -v -u admin:admin -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d'{"type":"page","title":"new page","space":{"key":"DEMO"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' "http://localhost:1990/confluence/rest/api/content/?os_authType=basic"

如果你想在頁面中添加一個特定頁面的孩子,你需要知道父母的pageId

curl -v -u admin:admin -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d'{"type":"page","ancestors":[{"type":"page","id":1048582}],"title":"third new child page","space":{"key":"DEMO"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' "http://localhost:1990/confluence/rest/api/content/?os_authType=basic"

有關更多詳細信息,請參見Scott Dudley's answer to a previous question

此外,我建議使用Confluence REST API Browser來測試您的呼叫。


更新:

下面是一些示例代碼 - 改編自here

  1. 加深對Apache HttpClient的,這將使你做出了規定要求
  2. 創建HttpPost請求並添加標題「application/x-www-form-urlencoded」
  3. 創建一個StringEntity,你將通過JSON它
  4. 執行調用

代碼大致樣子(你仍然需要調試它,讓它工作)

HttpClient httpClient = new DefaultHttpClient(); 

try { 
    HttpPost request = new HttpPost("http://localhost:1990/confluence/rest/api/content/?os_username=admin&os_password=admin"); 
    StringEntity params = new StringEntity("{"type":"page","ancestors":[{"type":"page","id":1048582}],"title":"third new child page","space":{"key":"DEMO"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}"); // properly escape this 
    request.addHeader("content-type", "application/json"); 
    request.setEntity(params); 
    HttpResponse response = httpClient.execute(request); 

    // handle response here... 
}catch (Exception ex) { 
    // handle exception here 
} finally { 
    httpClient.getConnectionManager().shutdown(); 
} 

如果您已經創建一個具有正確JSON結構的POJO,您可以使用GSON libray(或其他)將Java對象轉換爲JSON,而不是從字符串手動構建JSON。

+0

感謝comment.i通過你的鏈接,現在我對此有一些更清楚的了,但是在編程的角度來看,這對我來說沒有任何幫助。現在我能夠在頁面中插入數據,但無法將數據插入到表中。我的問題現在有點change.so我更新我的問題。所以請看到它,並給出一些答案像上面的答案。 – Mayank 2014-11-07 07:44:14

+0

很抱歉對一個老問題發表評論,但在搜索如何更新Confluence中的表時發現此頁。訣竅是啓用html宏,並以這種方式插入你的表(不安全)*或*,將表示格式更改爲'wiki'(從上面的答案中的'storage'),並使用其中一個支持標記這裏列出的表的wiki標記:https://confluence.atlassian.com/doc/confluence-wiki-markup-251003035.html – unickshaxor 2015-11-12 17:30:44

相關問題