Liferay 是基于 SOA 理念设计的,很容易通过 Web Services 对外提供服务接口,下面简单介绍一下。
Liferay 如何对外提供服务?
1 、在 service.xml 中编辑,增加一个 <entity name="xx" local-service="false" remote-service="true" />
2 、 ant build-service-xxxx (portal-impl/build.xml)
3 、修改 XXServiceImpl, 写入你要对外提供的方法逻辑;
4 、 ant build-service-xxxx (重复 2 )
5 、 ant build-wsdd-xxxx in portal-impl/build.xml
6 、 ant clean deploy in portal-impl/build.xml
这样你就成功发布以了一个服务,在 tunnel-web/doc-root/WEB-INF/server-config.wsdd 中查找是否发布成功
如何调用 Liferay 发布的服务? smilingleo 原创
1 、新建一个项目(或者打开你要调用服务的项目)
2 、将 Apache AXIS 的所有 lib 文件拷贝到 <your-webapp>/WEB-INF/lib 下面;
3 、将 portal-client.jar 拷贝到上述目录,如果没有,在 portal-client/build.xml 中 ant build-client (注意,这时,服务器要在开启状态,而且上面编写的服务已经成功 deploy 到服务器)
4 、编写代码,例如;
import java.net.URL;
import com.company.portal.service.http.MyUserServiceSoap;
import com.company.portal.service.http.MyUserServiceSoapServiceLocator;
public class LiferayClient ...{
public static void main(String [] args) ...{
long userId = 54321L;
long companyId = 12345L;
String email = "[email protected]";
String password = "notTellingYou";
try ...{
MyUserServiceSoapServiceLocator locator = new MyUserServiceSoapServiceLocator();
MyUserServiceSoap soap = locator.getPortal_MyUserService(_getURL(Long.toString(userId), "Portal_MyUserService"));
int isAuthenticated = soap.authenticateByEmailAddress(companyId, email, password);
System.out.println("is user authenticated? " + isAuthenticated);
} catch (Exception e) ...{
System.err.println(e.toString());
}
}
private static URL _getURL(String remoteUser, String serviceName) throws Exception ...{
String password = "secret";
url = "http://" + remoteUser + ":" + password + "@localhost:8080/tunnel-web/secure/axis/" + serviceName;
return new URL(url);
}
}