Showing posts with label Web Service. Show all posts
Showing posts with label Web Service. Show all posts

Thursday, March 6, 2014

How to create Spring based web service client with Apache CXF

In previous article, i have explained how to develop the Web Service using the Apache CXF framework. today i am planning to demonstrate on developing spring based web service client using Apache CXF.


In here, i will be developing a web service client for the above web service. you may follow the following steps.

first of all, you need to make sure that your web service is up and running.
if so, you can follow the steps given below to create your spring based web service client.


1. first create a maven based project. i have created a maven based simple java application.


2. Then add the spring dependency for the pom.xml

3. Then add the Apache CXF dependencies for the pom.xml


eventually your pom.xml will looks as below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chathurangaonline.apache.cxf.jaxws.spring.samples</groupId>
    <artifactId>apache-cxf-jaxws-spring-jaxws-spring-client</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>apache-cxf-jaxws-spring-jaxws-spring-client Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <cxf.version>3.0.4</cxf.version>
        <spring.version>4.0.2.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!--spring dependencies-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--apache cxf dependencies-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>apache-cxf-jaxws-spring-jaxws-spring-client</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>




4. Now generate the client side libraries/stubs that required for the web service client from the WSDL of the service. then integrate the generated client stub files with your client application. this can be done with the following command.
if you need more information, you can refer this article for it.

wsimport  -keep  -verbose -d /home/chathuranga/Projects/jax-ws-tutorial/jax-ws-jaxb-sample-app/webClient/jaxb-client/src/main/java/  http://localhost:8080/employee-service/empServiceUrl?wsdl


5. create a applicationContext.xml file in the resource folder and add the following configurations there.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:property-placeholder location="classpath*:configuration.properties"/>

    <jaxws:client id="calcServiceBean"
                  serviceClass="com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl.CalculatorService"
                  address="${web.service.multiplication.url}" />


</beans>


6. create the configuration.properties file in resources directory and add the following entry to it.

 web.service.multiplication.url = http://localhost:8080/apache-cxf-jaxws-spring-sample/calcService


7. now you can create the client as follows.

package com.chathurangaonline.apache.cxf.jaxws.client.samples;

import com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl.CalculatorService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CXFTestClient {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        CalculatorService calculatorService = (CalculatorService) applicationContext.getBean("calcServiceBean");
        double answer =  calculatorService.multiply(10,22);
        System.out.println(" answer is ["+answer+"]");
    }
}



you can download the fully source code from the following repository.

Source Code :- https://github.com/chathurangat/apache-cxf-jaxws-spring-client

Hope this will be helpful for you!

Thanks
Chathuranga Tennakoon
www.chathurangaonline.com


Friday, February 28, 2014

Apache CXF Spring Integration Example

Apache CXF is a robust web service framework written in java that fully supports for the Spring based web service development. the purpose of this article is to share my experience on Apache CXF web service development on Spring framework. this example has been well tested with SoapUI.

the project structure will be as follows.




pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chathurangaonline.apache.cxf.jaxws.spring.samples</groupId>
    <artifactId>apache-cxf-jaxws-spring-sample</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>apache-cxf-jaxws-spring-sample Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <cxf.version>3.0.4</cxf.version>
        <spring.version>4.0.2.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>apache-cxf-jaxws-spring-sample</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>



CalculatorService.java


package com.chathurangaonline.apache.cxf.jaxws.spring.samples;

import javax.jws.WebService;

/**
 * <p>
 *     SIB
 * </p>
 * @author Chathuranga Tennakoon / www.chathurangaonline.com
 */
@WebService
public interface CalculatorService {

    double multiply(double num1,double num2);
}



CalculatorServiceImpl.java


package com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl;

import com.chathurangaonline.apache.cxf.jaxws.spring.samples.CalculatorService;
import javax.jws.WebService;

/**
 * <p>
 *     SIB for {@link com.chathurangaonline.apache.cxf.jaxws.spring.samples.CalculatorService}
 * </p>
 * @author Chathuranga Tennakoon / www.chathurangaonline.com
 */
@WebService
public class CalculatorServiceImpl implements CalculatorService {

    @Override
    public double multiply(double num1, double num2) {
        return num1 * num2;
    }
}



web.xml


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>AppCXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>AppCXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>


applicationConxtext.xml


<?xml version="1.0" encoding="UTF-8"?>
<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">


    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint id="calServiceEndpoint"
                    implementor="com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl.CalculatorServiceImpl"
                    address="/calcService"/>

</beans>



Once the web service is successfully deployed on web server(in my case it is tomcat), you can access the WSDL from following url.

http://localhost:8080/apache-cxf-jaxws-spring-sample/calcService?wsdl


The source code can be downloaded at:-
Get Source code From GitHub


Thanks
Chathuranga Tennakoon
www.chathurangaonline.com





Friday, February 14, 2014

JAX-WS Web Service with SOAP 1.2

JAX-WS uses the SOAP 1.1 for the Web Services if the SOAP version is not explicitly defined (By Default). this is the default behavior.  if you want to use SOAP 1.2 for your web service, then you may need to explicitly declare it in the web service. then following annotation can be used to explicitly define the SOAP version of the web service.

For SOAP 1.2

@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING) 


Sample Java Implementation

package com.chathurangaonline.sample.jaxws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

/**
 * <p>
 *     Service Implementation Bean (SIB) for CalculatorService endpoint interface
 * </p>
 */
@WebService
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
public class CalculatorServiceImpl implements CalculatorService{


    @WebMethod
    public long add(long number1, long number2) {
        return number1+number2;
    }

    @WebMethod
    public long subtract(long number1, long number2) {
        return number1+number2;
    }

    @WebMethod
    public long multiply(long number1, long number2) {
        return number1*number2;
    }
}
 
Hope this will be helpful for you!

Thanks and Regards

Chathuranga Tennakoon
www.chathurangaonline.com

Monday, July 22, 2013

Simple JAX-WS Web Service (SOAP based) for Beginners


Today, i am going to explain simple JAX-WS web service( based on SOAP) for the beginners who are new to web service development. 
Web Services can be divided into two categories.
1. SOAP based web services
2. RESTful web services.

JAX-WS supports for both SOAP based and RESTful web services. In this post, we are going to discuss about the SOAP based web services. AS an entry point for the SOAP web service, first we will discuss the architecture of the typical SOAP based web service.


 


Here you can see that the SOAP client will communicate with the SOAP service with the aid of the SOAP libraries. SOAP libraries will be responsible to send and receive request and response between client and server. Here the both request and response will be the SOAP documents and those will be exchanged between client underlying SOAP libraries and the Service underlying SOAP libraries. These SOAP libraries will do the marshalling and unmarshaling whenever required.

What is marshalling and unmarshalling?

Marshalling – converting Java objects into the XML files.
Unmarshalling - converting XML files into the Java objects.

JAX-WS uses JAXB (Java Architecture for XML Binding) for the marshalling and unmarshalling.


In this example, we are going to look at the simple SOAP based 
web service that prints the hello concatenating with the provided 
web service input parameter as the output. Refer the below project 
tree structure to identify the components associated with the 
web service.


 
I will briefly explain the web service concepts by getting the
 examples from above sample web service.
  • Service Endpoint Interface (SEI) – HelloWorld.java 
  • Service Implementation Bean (SIB) – HelloWorldImpl.java
  • Web Service Publisher - HelloWorldPublisher.java 
Then we need to look at SEI, SIB and WsPublisher in detail.


SEI : - Service  Endpoint Interface
first of all you need to know what is the SEI. SEI is just a Java interface
that defines/declares all the web service methods. In this example,
the following file will be the SEI.

HelloWorld.java
package com.jax.ws.samples;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * <p>
 *     this will be the SEI that declares all the web service methods
 * </p>
 */
@WebService
public interface HelloWorld {

    @WebMethod
    String printWelcomeMessage(String username);
} 
SIB :- Service Implementation Bean SIB is the implementation of SEI. That means in the SIB, all the 
web methods declared in the SEI will be implemented.
SIB can be implemented as two ways.
 
 1. POJO class
2. Stateless Session EJB

In this example will be using POJO class as SIB to implement SEI as follows.

HelloWorldImpl.java

package com.jax.ws.samples;

import javax.jws.WebService;

/**
 * <p>
 *     this will be the SIB that implements all the web service methods
 *     declared in the SEI.
 * </p>
 */
@WebService(endpointInterface = "com.jax.ws.samples.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

    @Override
    public String printWelcomeMessage(String username) {
        return "Hello "+username;
    }
}
as you can see, the end point interface will be the relevant java 
interface (SEI) that is implemented by the relevant SIB.


Publishing the Web Service 
once the SEI and SIB have been completed  and compiled, the web 
service is ready to be published. In production mode,web service 
should be published using the application servers such as Tomcat, 
JBoss, WebLogic etc... but for the testing purpose it is possible to 
publish the web service with the simple java program as follows.

HelloWorldPublisher.java

package com.jax.ws.samples;

import javax.xml.ws.Endpoint;
/**
 * <p>
 *     this will be the sample web service publisher class
 *     whose responsibility to publish the given web service
 * </p>
 */
public class HelloWorldPublisher {

    public static void main(String []args){
        //1st argument - web service publication URL
        //2nd argument - instance of SIB
        Endpoint.publish("http://localhost:6666/sayHello",new HelloWorldImpl());
    }
}

you can publish the web service by running this simple program (HelloWorldPublisher.java). After running the publisher program, it is time to check whether the 
published web service is up and running. This can be done with web 
browser by getting the related WSDL document as follows.
 
http://localhost:9999/sayHello?wsdl


I think this will be helpful for you to start your first Java web service development.

This sample web service is available in the following GitHub repository. 
https://github.com/chathurangat/jax-ws-helloworld
 
I think this will give you an brief idea about how to develop web services with java. 
Hope this will helpful for you
 
Thanks and Regards
Chathuranga Tennakoon
 

Tuesday, April 10, 2012

WSDL (Web Service Description Language) Binding in WebService

WSDL (Web Service Description Language ) describes the web service message format and the protocol details. WSDL style binding describes how the WSDL details are bounded to the web service.  There are two WSDL binding styles.

1. RPC (Remote Procedure Call) Style Binding
2. Document Style Binding

http://publib.boulder.ibm.com/infocenter/dmndhelp/v6rxmx/index.jsp?topic=/com.ibm.wbit.help.ui.doc/topics/rwsdlstyle.html