如何使用Java在Atlassian Confluence中的页面表中插入数据

时间:2014-10-31 12:34:49

标签: java jsoup httpclient confluence

我编写了一个Java程序(仅使用main方法),用于使用 JSON 发送HTML内容。 我在我的对象中有一些数据在单独的程序中,我想在我的Atlassian Confluence页面中以编程方式导入。 我仍然只能在页面正文中插入静态html内容和数据。 我以前从未和json一起工作过。 如何在表格行中插入数据。



public class Example {

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

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

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

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

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

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

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

** 如果有人分享任何样本代码,对我来说非常有帮助。 我的页面中有以下表格。

__________________________________________ |

| S.no |书名|书作者|发布日期|价格
| _____ | ________ | ___________ | __________ | ______ |

1 个答案:

答案 0 :(得分:2)

您可以使用Confluence REST API执行此操作。从Confluence 5.5开始,有一个新的REST API。这完全是documented here

特别要考虑这个终点:

发布: /rest/api/content

内容类型:application/json

<强> BODY:

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

以下是一个在空间顶层添加页面的示例:

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. 创建一个将JSON传递给它的StringEntity
  4. 执行电话
  5. 代码大致看起来像(你仍然需要调试它并让它工作)

    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。