2014-10-31 20 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