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.setId(1L);
 user.setName("Test");
 user.setWeight(new BigDecimal("76"));
 return user;
}

public static void main(String[] args) throws MalformedURLException {
 final String TARGET_NAMESPACE = "http://www.cfx.ws/";
 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"));
 System.out.println(echoStub.echoHiToUser(getUserInstance()));
}
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:/
/cxf.apache.org}TransportURIResolver.http-conduit' has been configured for plain
 http.
 891  [main] DEBUG org.apache.cxf.transport.http.Headers  - Accept: */*
 1000 [main] INFO  org.apache.cxf.service.factory.ReflectionServiceFactoryBean  
- Creating Service {http://www.cfx.ws/}EchoImplService from WSDL: http://localho
st:8080/WithSpring/services/echo?wsdl
 1375 [main] DEBUG org.apache.cxf.jaxb.JAXBDataBinding  - 
Classes known to this context:
...
  com.code.first.ws.server.User
  com.code.first.ws.server.jaxws_asm.EchoHi
  com.code.first.ws.server.jaxws_asm.EchoHiResponse
  com.code.first.ws.server.jaxws_asm.EchoHiToUser
  com.code.first.ws.server.jaxws_asm.EchoHiToUserResponse
...
 no HandlerChain annotation on class javax.xml.ws.Service
 1531 [main] DEBUG org.apache.cxf.jaxws.ServiceImpl  - created proxy
 1531 [main] DEBUG org.apache.cxf.endpoint.ClientImpl  - Invoke, operation info:
 [BindingOperationInfo: {http://server.ws.first.code.com/}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
tp://www.cfx.ws/}EchoImplPort.http-conduit
...
 1875 [main] DEBUG org.apache.cxf.ws.policy.PolicyVerificationInInterceptor  - 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