jueves, 16 de junio de 2011

How to consume a Web Service in a Portlet Project with Eclipse

For this example I'm using Eclipse Helios and Ubuntu 10.04
First of all, download CXF2 from http://www.apache.org/dyn/closer.cgi?path=/cxf/2.4.1/apache-cxf-2.4.1.zip and extract it in any place of your computer.


Next,I suggest that you install the WTP plugins for Eclipse to continue




After that, we have to configure CXF2 enviroment. We have to open preferences from Window menu.
In CXF2.x Preferences we have to set the CXF2 runtime environment. This target is the place where we extracted CXF2




After that, we have to create a Portlet Project.




Next, click on Advanced project configuration...

Check CXF 2.x Web Services, click ok


Click Next

Select Liferay MVC and check Create custom portlet class.


Set the name for the portlet class, package and superclass(javax.portlet.GenericPortlet)
Click Next


Click Next


Click Next





Next, click on Finish.


After that, create a Web Service Client into the project


Set the Service Definition as: http://www.webservicex.net/globalweather.asmx?WSDL .In addition, set the Web service runtime as Apache CXF 2.x And set the service scale to Start Client. After that, click on Next



set the Package name to net.webservicex.example. Click on Next

Click next



Click on Start Server




Wait until the Server starts. Next, click on Finish

Open com.example.Weather.java and change doView function to:
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
try {
GlobalWeather service = new GlobalWeather();
GlobalWeatherSoap port = service.getGlobalWeatherSoap();
// TODO initialize WS operation arguments here
String cityName = "Quito";
String countryName = "Ecuador";
// TODO process result here
String result = port.getWeather(cityName, countryName);
renderRequest.setAttribute("info", result);
System.out.println("Result = " + result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
include(viewJSP, renderRequest, renderResponse);
}




Also, change view.jsp to print the result of the WS invocation.
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

${info}


Finally, Run the project on Liferay Server and add the portlet.


No hay comentarios:

Publicar un comentario