Liferay研究之十五:Liferay如何对外提供Service,以及如何调用

来源:互联网 发布:java编程入门 pdf 编辑:程序博客网 时间:2024/05/22 01:32

Liferay是基于SOA理念设计的,很容易通过Web Services对外提供服务接口,下面简单介绍一下。

Liferay如何对外提供服务?

1、在service.xml中编辑,增加一个<entity name="xx" local-service="false" remote-service="true" />

2ant build-service-xxxx (portal-impl/build.xml)

3、修改XXServiceImpl, 写入你要对外提供的方法逻辑;

4ant build-service-xxxx (重复2

5ant build-wsdd-xxxx in portal-impl/build.xml

6ant 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.xmlant 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 = "me@company.com";


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);


}

}

原创粉丝点击