Steps to Create Custom service and JSONWS API
Step1 :Create the service builder
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder
PUBLIC "-//Liferay//DTD
Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="co.uk.india">
<author>muniraj.saravanan</author>
<namespace>india</namespace>
<entity name="UserAdditionalInfo"
table="USER_INFORMATION" local-service="true" remote-service="true">
<!-- PK fields
-->
<column name="UserAdditionalInfoId"
type="long" primary="true"
/>
<!-- Audit
fields -->
<column name="userId" type="long"
/>
<column name="AccountingServices"
type="boolean" />
<column name="buinessInformation" type="boolean"
/>
<column name="companyFormation"
type="boolean" />
<column name="companyRegNumber"
type="String" />
<column name="contactOrganisationId"
type="long" />
<column name="telephoneNumber"
type="String" />
<column name="conveyancing"
type="boolean" />
<column name="costCentre"
type="String" />
<column name="legalPublications"
type="boolean" />
<column name="legalSupport"
type="boolean" />
<column name="organizationId"
type="long"/>
<column name="organizationName"
type="String"/>
<column name="outsourcingAndCompanyCompliance"
type="boolean" />
<column name="sopAccountNumber"
type="String"/>
<column name="trustAndCompanyServices"
type="boolean" />
<column name="vatRegNumber"
type="String"/>
<column name="webServices"
type="boolean" />
<column name="validated"
type="boolean" />
<column name="modifiedDate"
type="Date" />
<column name="createdDate"
type="Date" />
<!-- Order
-->
<order by="asc">
<order-column name="createdDate"
/>
</order>
<!-- Finder
methods -->
<finder name="userId"
return-type=" UserAdditionalInfo ">
<finder-column name="userId"
/>
</finder>
</entity>
</service-builder>
You change the entity name as desired, Based on the name
class will generate.
Build the service using
liferay service builder
.
Step 2: Configuring web.xml
To
make sure that your custom portlets can be scanned, and their service can
become part of the JSON API. For this you must add the following in portlets
web.xml:
1.
<servlet>
2.
<servlet-name>JSON Web Service Servlet</servlet-name>
3.
<servlet-class>com.liferay.portal.kernel.servlet.PortalClassLoaderServlet</servlet-class>
4.
<init-param>
5.
<param-name>servlet-class</param-name>
6.
<param-value>com.liferay.portal.jsonwebservice.JSONWebServiceServlet</param-value>
7.
</init-param>
8.
<load-on-startup>0</load-on-startup>
9.
</servlet>
10.
<servlet-mapping>
11.
<servlet-name>JSON Web Service Servlet</servlet-name>
12.
<url-pattern>/api/jsonws/*</url-pattern>
13.
</servlet-mapping>
14.
<servlet-mapping>
15.
<servlet-name>JSON Web Service Servlet</servlet-name>
16.
<url-pattern>/api/secure/jsonws/*</url-pattern>
17.
</servlet-mapping>
Step 3: Writing a custom
method to be exposed
a)
Add
the custom implementation method in <EntityName>LocalServiceImpl.java
For
example:
public class UserAdditionalInfoLocalServiceImpl
extends
UserAdditionalInfoLocalServiceBaseImpl {
public String addUserInfo(String companyRegNumber, String
organizationName) throws PortalException,
SystemException {
UserAdditionalInfo userAdditionalInfo = new
UserAdditionalInfoImpl();
userAdditionalInfo.setUserAdditionalInfoId(CounterLocalServiceUtil.increment(UserAdditionalInfo.class.getName()));
userAdditionalInfo.setOrganizationName(organizationName);
userAdditionalInfo.setCompanyRegNumber(companyRegNumber);
addUserAdditionalInfo(userAdditionalInfo);
return "Successfully Added ";
}
}
b)
And
add the custom method in <EntityName>ServiceImpl.java to the call the
implementation method written in <EntityName>LocalServiceImpl.java
For
example:
public class
UserAdditionalInfoServiceImpl
extends
UserAdditionalInfoServiceBaseImpl {
public String
addUserInfo(String companyRegNumber,
String organizationName) throws PortalException, SystemException {
return userAdditionalInfoLocalService.addUserInfo(companyRegNumber,
organizationName);
}
Step 4: rebuild the
service using liferay service builder and deploy the custom portlet.
Step 5: Consuming JSON
Web Service
To
list registered services on portlet,
1.
http://localhost:8080/<portlet-context>/api/jsonws
example :
http://localhost:8080/MyAccount-portlet/api/jsonws/
Does it works for LR 6.2 GA2?
ReplyDeleteI think not, or I've miss something
ReplyDeleteHi, Can I return a custom bean? For example:
ReplyDeletepublic MyCustomBean addUserInfo(String companyRegNumber, String organizationName) throws PortalException, SystemException { //code }
Thanks