Continuing from my previous post on (CXF Example –Web Service Using Spring and Maven), let's create a client application to consume the web service.
Steps
- Generate POJOs from WSDL to access the Web Service using the tool wsdl2java as below.
You can access the WSDL used in this example from here.
wsdl2java rateServiceWSDL.xml
WSDL2Java is a command line tool that generates Java classes from an existing WSDL document. Generated classes represent client stubs, server skeletons and data types that will helps you to write client side and server Java programs for Web services defined in the WSDL document.
- Create a client-beans.xml as below:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client
id="rateServiceClient"
serviceClass="com.mycompany.webservice.service.InterestRateService"
address="http://localhost:8080/rateservice-1.0/webservices/interestRate"
/>
</beans>
- Create the client Class to access the Web Service as below:
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(
new String[] { "com/mycompany/webservice/client-beans.xml" });
InterestRateService client = (InterestRateService) context
.getBean("rateServiceClient");
ArrayOfMortgageType arrayOfTypes = client.getMortgageTypes();
List<MortgageType> types = arrayOfTypes.getMortgageType();
for (MortgageType aType: types){
System.out.println(aType.getDescription().getValue());
}
You may download the complete source code from
Comments