Ibm sbt 如何使用Social Business Toolkit Java API创建子单元?
在SDK Javadoc中,Community类没有“setParentCommunity”方法,但CommunityList类有一个getSubCommunities方法,因此必须有一种编程方式来在创建新社区时设置父社区的Uuid。RESTAPI提到了一个“rel=”http://www.ibm.com/xmlns/prod/sn/parentcommunity“要素”。在寻找线索的同时,我检查了现有子单元的XmlDataHandler节点,发现了一个link元素。我尝试为一个新创建的社区获取XmlDataHandler,并添加一个链接节点,其中的href、rel和type节点与现有社区中的节点类似,但在尝试更新或重新保存社区时,我遇到了一个错误的请求错误。实际上,甚至当我尝试调用dataHandler.setData(n)时,其中n被设置为节点n=dataHandler.getData();在没有任何更改的情况下,调用updateCommunity或save时,我得到了相同的错误,因此操作dataHandler XML似乎是无效的Ibm sbt 如何使用Social Business Toolkit Java API创建子单元?,ibm-sbt,Ibm Sbt,在SDK Javadoc中,Community类没有“setParentCommunity”方法,但CommunityList类有一个getSubCommunities方法,因此必须有一种编程方式来在创建新社区时设置父社区的Uuid。RESTAPI提到了一个“rel=”http://www.ibm.com/xmlns/prod/sn/parentcommunity“要素”。在寻找线索的同时,我检查了现有子单元的XmlDataHandler节点,发现了一个link元素。我尝试为一个新创建的社区获取
创建新社区时,建议使用什么方法指定父社区,以便将其创建为子社区?以编程方式创建子社区的正确方法是修改社区创建的请求后正文-以下是指向Connections 45信息中心的链接-
SBT SDK中不支持使用CommunityService API执行此操作。我们需要使用使用Endpoint和ClientService类的低级Java API来直接调用具有适当请求主体的REST API。我将继续并扩展类CommunityService 然后继续添加社区服务 第605行 公共字符串CreateComunity(Community Community)引发CommunityServiceException{ if(null==社区){ 抛出新的CommunityServiceException(null,Messages.NullCommunityObjectException); }
try {
Object communityPayload;
try {
communityPayload = community.constructCreateRequestBody();
} catch (TransformerException e) {
throw new CommunityServiceException(e, Messages.CreateCommunityPayloadException);
}
String communityPostUrl = resolveCommunityUrl(CommunityEntity.COMMUNITIES.getCommunityEntityType(),CommunityType.MY.getCommunityType());
Response requestData = createData(communityPostUrl, null, communityPayload,ClientService.FORMAT_CONNECTIONS_OUTPUT);
community.clearFieldsMap();
return extractCommunityIdFromHeaders(requestData);
} catch (ClientServicesException e) {
throw new CommunityServiceException(e, Messages.CreateCommunityException);
} catch (IOException e) {
throw new CommunityServiceException(e, Messages.CreateCommunityException);
}
}
您将要更改您的CommunitySturl以匹配。。。
这里的Uuid是父Uuid。我按照@PaulBastide的建议创建了一个SubCommunityService类,目前只包含一个用于创建的方法。它包装了CommunityService,而不是将其子类化,因为我发现它更可取。下面是代码,以防您想要重用它:
public class SubCommunityService {
private final CommunityService communityService;
public SubCommunityService(CommunityService communityService) {
this.communityService = communityService;
}
public Community createCommunity(Community community, String superCommunityId) throws ClientServicesException {
Object constructCreateRequestBody = community.constructCreateRequestBody();
ClientService clientService = communityService.getEndpoint().getClientService();
String entityType = CommunityEntity.COMMUNITY.getCommunityEntityType();
Map<String, String> params = new HashMap<>();
params.put("communityUuid", superCommunityId);
String postUrl = communityService.resolveCommunityUrl(entityType,
CommunityType.SUBCOMMUNITIES.getCommunityType(), params);
String newCommunityUrl = (String) clientService.post(postUrl, null, constructCreateRequestBody,
ClientService.FORMAT_CONNECTIONS_OUTPUT);
String communityId = newCommunityUrl.substring(newCommunityUrl.indexOf("communityUuid=")
+ "communityUuid=".length());
community.setCommunityUuid(communityId);
return community;
}
公共类子单元服务{
私人最终社区服务;
公共子单元服务(社区服务社区服务){
this.communityService=社区服务;
}
公共社区createCommunity(社区社区,字符串超级社区ID)引发ClientServicesException{
对象constructCreateRequestBody=community.constructCreateRequestBody();
ClientService ClientService=communityService.getEndpoint().getClientService();
字符串entityType=CommunityEntity.COMMUNITY.getCommunityEntityType();
Map params=新的HashMap();
参数put(“社区UUID”,超级社区ID);
String postUrl=communityService.resolveCommunityUrl(entityType,
CommunityType.SUBCOMMUNITIES.getCommunityType(),参数);
String newCommunityUrl=(String)clientService.post(postrl,null,constructCreateRequestBody,
ClientService.FORMAT_CONNECTIONS_OUTPUT);
字符串communityId=newCommunityUrl.substring(newCommunityUrl.indexOf(“communityUuid=”)
+“communityUuid=“.length());
community.setCommunityUuid(communityId);
返回社区;
}
}我不确定,但我会请Manish回复您应该在GitHub上提出问题请求-是否有可用的代码示例?该链接上的文档为我指明了“正确的方向”但是非常小。效果很好!。我所做的唯一更改是:1.communityService.resolveCommunityUrl在源代码中受保护,因此创建了一个“MyCommunityService”类,该类扩展了communityService并公开了该方法,然后引用了myCommunityService而不是communityService 2。clientService.post的响应需要是响应对象/无法直接转换为字符串很高兴听到它起作用了!我基于SBT的较旧版本实现了该方法,因此您提到的事情在此期间发生了变化。如果您不坚持使用公共API,这是一个折衷方案;在正式实现之前,这仍然是一个很好的解决方案。
try {
Object communityPayload;
try {
communityPayload = community.constructCreateRequestBody();
} catch (TransformerException e) {
throw new CommunityServiceException(e, Messages.CreateCommunityPayloadException);
}
String communityPostUrl = resolveCommunityUrl(CommunityEntity.COMMUNITIES.getCommunityEntityType(),CommunityType.MY.getCommunityType());
Response requestData = createData(communityPostUrl, null, communityPayload,ClientService.FORMAT_CONNECTIONS_OUTPUT);
community.clearFieldsMap();
return extractCommunityIdFromHeaders(requestData);
} catch (ClientServicesException e) {
throw new CommunityServiceException(e, Messages.CreateCommunityException);
} catch (IOException e) {
throw new CommunityServiceException(e, Messages.CreateCommunityException);
}
}