mua nha bien hoa
Chào tất cả các bạn,
Bài viết này tôi sẽ giới thiệu về cách tạo webservice trên liferay. Thông thường thì webservice chi a làm hai loại
- Loại 1: Tạo webservice trên một portlet để kết nối tới một webservice server nào đó thì gọi là web service client
- Loại 2: Tạo một webservice để cung cấp dịch vụ cho các client truy cập vào thì gọi là webservice server
Bài viết dưới đây của tôi áp dụng cho loại thứ 2 vì loại 1 cũng đơn giản nên không nói đến.
Bước 1: Tạo một project bằng sdk-plugin của liferay
- Liferay runtime để là Liferay tomcat (Chính là bản tomcat đã tích hợp sẵn liferay vào rồi đó)
- Ấn vào Next
- Tại màn hình tiếp theo các bạn có thể chọn MVC Portlet
- Kích chuột phải vào plugin project và chọn New->Liferay Service.
- Bạn nhập đủ thông tin chi tiết như hình dưới
- Bạn có thể nhâp package và Namespace khác nếu bạn muấn.
- Ấn Finish. File service.xml sẽ được tạo ra và ngầm định một số trường cơ bản, các bạn có thể sửa lại tên entry và field name nếu các bạn muấn
- Tại File service.xml này chúng ta có thể khai báo nhiều bảng và nhiều field tương ứng trong mỗi một Entry
- Tại đây tôi chỉ tạo ra 2 trương id,name để thuận tiện cho việc demo
- Sau khi các bạn đã sửa xong thì hãy lưu lại
- Kéo fiel build.xml tới màn hình ant
- Trong màn hình ant nó liệt kê ra một danh sách các bạn hãy click 2 phát vào build-service là nó sẽ tự động build và sinh ra một số class cơ bản cho các bạn.
- Nếu như có lỗi các bạn hãy thực hiện lại từ đầu
- Sauk hi build thành công các bạn hãy Refresh project (F5) to see the files.
- Right click on the project go to Properties->Java Build Path->Source tab. Add the TestPlugin-portlet/docroot/WEB-INF/service and TestPlugin-portlet/docroot/WEB-INF/src folders, and click Ok.
- Thêm đoạn code dưới đây vào class này com.liferay.test.service.impl.EmployeeLocalServiceImplpublic Employee create(Employee model) throws SystemException{
long id = CounterLocalServiceUtil.increment(Employee.class.getName());
model.setId(id);
Employee newModel = EmployeeUtil.create(model.getId());
newModel.setName(model.getName());
EmployeeUtil.update(newModel, true);
return newModel;
}
public Employee update(Employee model) throws NoSuchEmployeeException, SystemException{
Employee newModel = EmployeeUtil.findByPrimaryKey(model.getId());
newModel.setName(model.getName());
EmployeeUtil.update(newModel, true);
return newModel;
}
public List<Employee> findByName(String name) throws SystemException{
return EmployeeUtil.findByName(name);
}
public Employee remove(long id) throws NoSuchEmployeeException, SystemException{
return EmployeeUtil.remove(id);
} - Sau khi thêm đoạn code này vào thì các bạn hãy build service lại một lần nữa
- Tương tự như vậy các bạn hãy thêm một đoạn code này vào class com.liferay.test.service.impl.EmployeeServiceImpl
public Employee create(Employee model) throws SystemException{
return EmployeeLocalServiceUtil.create(model);
}
public Employee update(Employee model) throws NoSuchEmployeeException, SystemException{
return EmployeeLocalServiceUtil.update(model);
}
public List<Employee> findByName(String name) throws SystemException{
return EmployeeLocalServiceUtil.findByName(name);
}
public Employee remove(long id) throws NoSuchEmployeeException, SystemException{
return EmployeeLocalServiceUtil.remove(id);
}
- Sau khi thêm đoạn code này vào thì các bạn hãy build service lại một lần nữa
- There is a bug in liferay's service builder due to which it doesn't generate the ServiceSoap class correctly. It is not able convert the parameters to their corresponding soap type. See http://issues.liferay.com/browse/LPS-12629 for details. To get around this issue open the EmployeeServiceSoap and change the generated create and update methods as:
Mỗi lần các bạn build service thì class này lại được sinh ra các hàm cần thiết và nó không đúng chuẩn để có thể build được webservice của bạn, cho nên các bạn hãy xóa con mẹ nó hết đi và copy đoạn code này vô
Public static com.liferay.test.model.EmployeeSoap create(
com.liferay.test.model.EmployeeSoap model) throws RemoteException {
try {
com.liferay.test.model.Employee returnValue = EmployeeServiceUtil.create(com.liferay.test.model.impl.EmployeeModelImpl.toModel(model));
returncom.liferay.test.model.EmployeeSoap.toSoapModel(returnValue);
}
catch (Exception e) {
_log.error(e, e);
throw newRemoteException(e.getMessage());
}
}
Public staticcom.liferay.test.model.EmployeeSoap update(
com.liferay.test.model.EmployeeSoap model) throws RemoteException {
try {
com.liferay.test.model.Employee returnValue = EmployeeServiceUtil.update(com.liferay.test.model.impl.EmployeeModelImpl.toModel(model));
return com.liferay.test.model.EmployeeSoap.toSoapModel(returnValue);
}
catch (Exception e) {
_log.error(e, e);
Throw newRemoteException(e.getMessage());
}
}
- Sauk hi các bạn sửa xong file trên thì tiếp tục build-wsdd để nó tự sinh ra phần code cho webservices của bạn.
- Ok, hoàn thành hết các bước trên các bạn có thể start server lên
- Thực hiện compile TestPlugin-Portlet và deploy TestPlugin-Portlet.(thao tác này thì có 2 cách để làm )
- Cách 1: add portlet mà bạn vừa tạo vào server trên màn hình của esclip
- Sử dụng ant để export portlet của bạn ra file war, sau đó copy war lên thu mục web app của server tomcat. - Kiểm tra logs xem có dòng này không "1 portlet for TestPlugin-portlet is available for use" , nếu có tức là bạn đã đăng ký portlet với liferay thành công.
- Thông thường thì liferay để cổng ngầm định là 8080, nên các bạn có thể truy cập vào url này thử xem http://localhost:8080 (trên trình duyệt bất kỳ)
- Nếu vào được các bạn sử dụng user test@liferay.com và password là test.
- Go to Add -> More->Samples. Add the TestPlugin to you page. Make sure that the default page for the plugin appears. This confirms that the plugin was deployed properly.
- Now open http://127.0.0.1:8080/TestPlugin-portlet/axis/Plugin_Test_EmployeeService?wsdl in your browser and confirm that the wsdl opens up.
- This completes the plugin service creation.
Bước 2: TạoWeb service client
- Now we need to test our webservice. For that we need to have another tomcat runtime as our Liferay tomcat 6 runtime eclipse plugin doesn't support building webservices. Also we need to make sure that the tomcat runtime and liferay runtime run on different ports. I used the ports 9005, 9080 and 9009 for my tomcat runtime.
- In eclipse go to New->Webservice Client and enter the details as in the screenshot below:
- Click Next on this and next screen. Then click on "Start server" to start the tomcat runtime. Then click Finish.
- Select all the methods and click Finish
- Eclipse will then deploy the client and open up a screen to test your webservice.
- Click on the methods to test them.
- We will test create method first. We will provide the primaryKey=0, name=Liferay and id=0 and click invoke.
- In the results section we can see that the employee entity has been created with the id=1.
- Now invoke findByName and provide name=Liferay. The result will show the reference of the entity object returned by the webservice.
Sorry các bạn thời gian có hạn nên mình cũng chưa dịch hết được mong các bạn chịu khó đọc.
Thanks




Không có nhận xét nào:
Đăng nhận xét