1.自定义实体向外提供webservice接口的步骤
在下面这个例子中我们将在数据库中创建一张表并提供一个通过webservice修改表的方法
1.1表结构简单定义为:
CREATE TABLE [dbo].[MyBook](
[bookid] [bigint] NOT NULL,
[bookname] [varchar](500) NULL,
CONSTRAINT [PK_MyBook] PRIMARY KEY CLUSTERED
(
[bookid] ASC
)
1.2 在\service.xml中
<?xml version="1.0"?> <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd"> <service-builder package-path="com.ext.portlet.mybook"> <namespace>MyBook</namespace> <entity name="MyBook" local-service="true" remote-service="true"> <column name="bookid" type="long" primary="true" /> <column name="bookname" type="String" /> </entity> <exceptions> <exception>BookEntryName</exception> </exceptions> </service-builder>
1.3 执行 ant build-service
Open the generated file MyBookServiceImpl.java. if your entity name is ‘MyEntity’ in service.xml the generated class name will MyEntityServiceImpl.java
1.4 打开自动生成 MyBookServiceImpl.java (当然你的entity名是MyEntity,这里生成的类名就是MyEntityServiceImpl.java )
public MyBook addBook(String bookName) throws PortalException, SystemException {
MyBook mybook = null;
long bookId = CounterLocalServiceUtil.increment(MyBook.class.getName());
mybook = MyBookUtil.create(bookId);
mybook.setBookname(bookName);
return MyBookUtil.update(mybook, false);
}
1.5 执行 ant build-wsdd 将会生成webservice层的代码
2 liferay webservice的调用
2.1在liferay环境中 启用webservice
liferay中使用的是 Apache Axis 生成的 webservice,这个默认的配置在 /portal/tunnel-web/ docroot/WEB-INF文件夹中的server-config.wsdd。在浏览器中 使用 http://127.0.0.1:8080/ tunnel-web/axis就可以看到SOAP services的列表。
要远程连接service,这个域名或ip必须被允许,而这个就需要在 portal-ext. properties文件中配置:
axis.servlet.hosts.allowed=127.0.0.1,SERVER_IP
axis.servlet.https.required=false
这段代码显示能够连接到Axis servlet 的ip地址,你也可以设置为 空 ,允许所有的ip
2.2使用 webservice 的简单例子
2.2.1 保证http://127.0.0.1:8080/tunnel-web/axis 能查看到service列表
2.2.2 连接wsdl , 点击任何service的wsdl 这里使用User Service wsdl 为例( http://127.0.0.1:8080/tunnel-web/axis/Portal_UserService?wsdl)
2.2.3 使用eclipse或netbeans 可以根据Wsdl生成调用 代码
2.2.4 下面是一个调用例子
public class LiferayUserServiceClient {
public static void main(String[] args) {
try {
UserServiceSoapServiceLocator locatorUser = new UserServiceSoapServiceLocator();
UserServiceSoap soapUser = locatorUser.
getPortal_UserService("http://abc:abc@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService");
UserSoap soapUserModel = soapUser.getUserById(10806);
System.out.println(" getEmailAddress:" + soapUserModel.getEmailAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
主要参考:http://arvindm.com/2010/03/23/web-services-in-liferay/
转载请注明出处:http://nilm61.iteye.com/