我编写了一个 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);
}
}
}
_________________________________________|
|编号 |书名|本书作者 |发布日期 |价格
|_____|________|___________|__________|______|
最佳答案
您可以使用 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 :
- 获取 Apache HttpClient,这将使您能够发出所需的请求
- 使用它创建 HttpPost 请求并添加 header “application/x-www-form-urlencoded”
- 创建一个 StringEntity,并将 JSON 传递给它
- 执行调用
代码大致如下(您仍然需要调试它并使其工作)
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 库(或其他库)将 Java 对象转换为 JSON,而不是从字符串手动构建 JSON。
关于java - 如何使用 Java 在 Atlassian Confluence 中的页面表中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26674842/