Search This Blog

Saturday, 13 April 2013

The first CXF Service - 2

In the previous post we created and deployed the Echo webservice. Now to test the same. For this I created a simple client.
private static User getUserInstance() {
 User user = new User();
 user.setWeight(new BigDecimal("76"));
 return user;

public static void main(String[] args) throws MalformedURLException {
 final String TARGET_NAMESPACE = "";
 final String endpointAddress = "http://localhost:8080/WithSpring/services/echo";
 final String wsdlUrl = endpointAddress + "?wsdl";
 URL wsdlLocation = new URL(wsdlUrl);

 final QName SERVICE_NAME = new QName(TARGET_NAMESPACE, "EchoImplService");
 final QName PORT_NAME = new QName(TARGET_NAMESPACE, "EchoImplPort");

 // Service objects provide the client view of a Web service.
 final Service service = Service.create(wsdlLocation, SERVICE_NAME);

 // Creates a new port for the service. Ports created in this way contain
 // no WSDL port type information and can only be used for creating Dispatchinstances.
 service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

 IEcho echoStub = service.getPort(IEcho.class);
 System.out.println(echoStub.echoHi("are u there"));
The code:
  1. created qualified names for the service and the port. 
  2. It created Service objects that provide the client view of a Web service. 
  3. It then created a new port for the service – indicating the messaging format and the endpoint address to use. 
  4. Next step was to acquire a stub from the service object. 
  5. It executed the two exposed operations I had made the IEcho and User classes available at the client. In a normal project the same would be achieved by generating the java classes from the wsdl.
The output at the client is as below (for the first call):
891  [main] DEBUG org.apache.cxf.transport.http.HTTPConduit  - Conduit '{http:/
/}TransportURIResolver.http-conduit' has been configured for plain
 891  [main] DEBUG org.apache.cxf.transport.http.Headers  - Accept: */*
 1000 [main] INFO  org.apache.cxf.service.factory.ReflectionServiceFactoryBean  
- Creating Service {}EchoImplService from WSDL: http://localho
 1375 [main] DEBUG org.apache.cxf.jaxb.JAXBDataBinding  - 
Classes known to this context:
 no HandlerChain annotation on class
 1531 [main] DEBUG org.apache.cxf.jaxws.ServiceImpl  - created proxy
 1531 [main] DEBUG org.apache.cxf.endpoint.ClientImpl  - Invoke, operation info:
 [BindingOperationInfo: {}echoHi], params: [are 
u there]
 1797 [main] DEBUG org.apache.cxf.transport.http.HTTPConduit  - Sending POST Mes
sage with Headers to http://localhost:8080/WithSpring/services/echo Conduit :{ht
 1875 [main] DEBUG  - V
erified policies for inbound message.
 1875 [main] DEBUG org.apache.cxf.phase.PhaseInterceptorChain  - Invoking handle
Message on interceptor org.apache.cxf.interceptor.StaxInEndingInterceptor@dd75a4
 Hi  are u there
This completes the client.

No comments:

Post a Comment