参考:http://www.huqiwen.com/2016/05/22/add-journal-article-in-liferay-with-webservice/
在Liferay中有时候我们希望通过在其他系统中或者远程发布网页内容,此时就需要使用Liferay提供的webservice接口,可能会碰到如下的问题:
1、发布后的内容是草稿状态;
2、发布后的内容不能在Asset Publisher里面看到;
3、发布时后台有时候会报错;
所有的问题归结起来就是没有传入正确的参数,主要是ServiceContext。核心代码如下:
JournalArticleServiceSoapService articleLocator = new JournalArticleServiceSoapServiceLocator();
ServiceContext serviceContext = new ServiceContext();
JournalArticleServiceSoap articleService = articleLocator
.getPortlet_Journal_JournalArticleService(APIUtil
.getPortalURL("Portlet_Journal_JournalArticleService"));
String[] titleLanguageIds = {"zh_CN"};
String[] titleValue = {"新闻测试42"};
String[] desLanguageIds = {"zh_CN"};
String[] desValue = {"这是内容的描述2"};
String content = buildContent("新闻内容");
String type="general";
serviceContext.setWorkflowAction(1);
serviceContext.setScopeGroupId(20181);
serviceContext.setIndexingEnabled(true);
JournalArticleSoap articleSoap = articleService.addArticle(20181, 0, 0, 0, "", true,
titleLanguageIds, titleValue, desLanguageIds, desValue, content, type, "", "", null, 4,
22, 2016, 8, 10, 0, 0, 0, 0, 0, true, 0, 0, 0, 0, 0, true, true, "",
serviceContext);
private static String buildContent(String content) {
String contentStr = "<?xml version='1.0' encoding='UTF-8'?><root available-locales="zh_CN"
default-locale="zh_CN"><static-content language-id="zh_CN"><![CDATA[|" + content + "]]>
</static-content></root>";
return contentStr;
特别需要注意的就是其中的标红部分,其他的参数可以对照API里面的参数名称进行即可。
方法体如下:
addArticle(groupId, folderId, classNameId, classPK, articleId, autoArticleId, titleMapLanguageIds, titleMapValues, descriptionMapLanguageIds, descriptionMapValues, content, type, ddmStructureKey, ddmTemplateKey, layoutUuid, displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute, expirationDateMonth, expirationDateDay, expirationDateYear, expirationDateHour, expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute, neverReview, indexable, articleURL, serviceContext)
参数说明:
groupId:站点ID,需要将内容发布在哪个站点;
folderId:此网页内容所属的文件夹,默认填0,如果需要指定文件夹,传入对应的ID;
classNameId:journalArticle类的classNameId,添加时为0;
classPk:类的主键,添加时为0;
articleId:String类型,默认为空;
autoArticleId:是否自动创建articleId,我们使用true,默认创建;
titleMapLanguageIds:标题的语言ID,参考上面的代码,是一个语言id的数组,如中文、英文等;
titleMapValues:标题的语言对应的内容,参考上面的代码,是一个String数组;
descriptionMapLanguageIds:描述的语言ID,参考上面的代码,String数组;
descriptionMapValues:描述对应的值,参考上面的代码,String数组;
content:正文,这里是一个XML的值,里面存的为对应结构的内容。
type:网页内容的类型,默认用general即可。
ddmStructureKey:结构的ID,如果是基本网页内容传入为0;
ddmTemplateKey:对应结构的模板ID;
layoutUuid:显示的目标页面的UUID;
后面的几个分别是显示日期、过期日期、重新编辑时期的年、月、日、小时、分钟,不再赘述;
注意:显示日期是必须的,如果是小于当前的时间就是立刻显示的,如果是大于当前的时间,比如设置为三天后显示,则在资源发布器里面,只有三天后才会看到,如果是小于当前的时间是可以立刻看到。
过期时间、重新编辑时间等类似。
indexable:是否索引,一般为true;
articleURL:传入空值即可。
serviceContext:见上面的代码。
APIUtil的代码是构建了一个URL,如下:
private static URL getURL(String serviceName,String project) throws MalformedURLException {
String url = "http://localhost:8080";
String email = "test";
String password = "test";
int pos = url.indexOf("://");
String protocol = url.substring(0, pos + 3);
String host = url.substring(pos + 3, url.length());
StringBuilder sb = new StringBuilder();
sb.append(protocol);
try {
sb.append(URLEncoder.encode(email, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
sb.append(":");
sb.append(password);
sb.append("@");
sb.append(host);
if (null!=project && !"".equals(project)) {
sb.append("/"+project);
}
sb.append("/api/axis/");
sb.append(serviceName);
System.out.println(sb.toString());
return new URL(sb.toString());
}