Home > Web Services > How to create SOAP WebService with spring, cxf, Maven, Java

How to create SOAP WebService with spring, cxf, Maven, Java

July 7, 2012

Steps to create MAVEN SOAP Based Web Service with Eclipse.

Pre-Requsites :

  1. Java >= 1.5 version
  2. Maven installation
  3. Eclipse Maven Plugin

Steps to create :

Create a new Maven Project with packaging type as war

open the pom file and maven and spring dependencies

pom.xml file


4.0.0
com.test
cxf-spring-test
0.1
war

UTF-8
3.0.5.RELEASE
1.6.4
2.4.1

maven2-repository.java.net
Java.net Repository for Maven
http://download.java.net/maven/2/
default

${project.artifactId}

maven-compiler-plugin
2.3.2

1.6
1.6

org.mortbay.jetty
jetty-maven-plugin
7.4.2.v20110526

10

7009

/${project.artifactId}

<!-- CXF Dependencies -->

jta
jta
1.0.1b

org.apache.cxf
cxf-rt-core
${cxf.version}

org.apache.cxf
cxf-rt-frontend-simple
${cxf.version}

org.apache.cxf
cxf-rt-frontend-jaxws
${cxf.version}

org.apache.cxf
cxf-rt-databinding-aegis
${cxf.version}

org.apache.cxf
cxf-rt-transports-local
${cxf.version}

org.apache.cxf
cxf-rt-transports-http
${cxf.version}

org.apache.cxf
cxf-rt-transports-http-jetty
${cxf.version}

org.apache.cxf
cxf-rt-transports-jms
${cxf.version}

org.apache.cxf
cxf-rt-management
${cxf.version}

org.apache.cxf
cxf-common-utilities
${cxf.version}

<!-- End of CXF Dependencies -->

org.eclipse.jetty
jetty-servlets
7.4.0.v20110414
provided

org.eclipse.jetty
jetty-client

<!-- Spring Dependencies ${spring.version} -->

org.springframework
spring-beans
${spring.version}

org.springframework
spring-jdbc
${spring.version}

org.springframework
spring-web
${spring.version}

org.springframework
spring-webmvc
${spring.version}

org.springframework
spring-orm
${spring.version}

org.springframework
spring-expression
${spring.version}

org.apache.geronimo.specs
geronimo-annotation_1.0_spec
1.1.1

org.slf4j
slf4j-log4j12
${slf4j.version}
jar
compile

Create a web.xml file in src/main/webapp/WEB-INF folder

In the web.xml mention the servlet mapping for cxf servlet and add spring listner to load the application context during the webapp startup time.

Web.Xml file

<!--?xml version="1.0" encoding="UTF-8"?-->

<!-- Servlet Mapping for CXFServlet -->

CXFServlet
org.apache.cxf.transport.servlet.CXFServlet

CXFServlet
/*

<!-- Context parameters -->

log4jConfigLocation
classpath:sitemonitor_log4j.properties

log4jExposeWebAppRoot
false

		org.springframework.web.util.Log4jConfigListener

contextConfigLocation
classpath:sitemonitor_appContext.xml

		org.springframework.web.context.ContextLoaderListener

Now create a package in the src/main/java folder as mentioned in the pom file

create a interface with the service you want to expose

package com.test;

import javax.jws.WebService;

@WebService
public interface CalculatorIntf
{
public long add(long num1,long num2);

public long subtract(long num1,long num2);

public long multiply(long num1,long num2);
}

Create a inplementation for this service and implement all the methods of the interface

package com.test;

public class CalculatorImpl implements CalculatorIntf
{
public long add(long num1, long num2)
{
return (num1 + num2);
}

public long subtract(long num1, long num2)
{
return (num1 - num2);
}

public long multiply(long num1, long num2)
{
return (num1 * num2);
}

}

create a spring application context file and add the file to the application class path.

Application Context file

<!--?xml   version="1.0" encoding="UTF-8"?-->

<!-- Initiliaing Client Webservices -->

<!-- JAX-WS Service Endpoint -->

now open the command prompt and navigate to the porject folder in the command prompt

run the maven command : mvn clean package jetty:run -DskipTests (for running the inbuild jetty we need to add this in the pom file.In the above pom has that jetty plugin)

now open the browser and hit http://localhost:7009/cxf-spring-test

now you should see the wsdl link.click on the wsdl link will show you the wsdl file.

you can test the service with soap-ui.

In the comming post i will post how to invoke this wsdl from the java program.

Advertisements
Categories: Web Services
  1. dxxvi
    July 9, 2012 at 10:59 pm

    I don’t see any indentation in the code.

    Like

  1. July 9, 2012 at 11:05 am
Comments are closed.
%d bloggers like this: