Archive

Archive for the ‘Web Services’ Category

Good Links

June 30, 2013 Comments off

Interview Preparation

Data Structures and Algorithms

Sample Questions: http://www.qubeet.com/

Linked List: http://giridhar-mb.blogspot.in/2012/11/linked-list-implementation-in-java.html?_sm_au_=iNsvkqGtW40Fn0st

Quick Sort: http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html?_sm_au_=iNsvkqGtW40Fn0st

Bubble Sort: http://www.cs.rit.edu/~atk/Java/Sorting/sorting.html?_sm_au_=iNsvkqGtW40Fn0st

All Sorts: http://thilinasameera.wordpress.com/2011/06/01/sorting-algorithms-sample-codes-on-java-c-and-matlab/?_sm_au_=iNsvkqGtW40Fn0st#_Insertion_Sort

Java

http://javarevisited.blogspot.in/

http://howtodoinjava.com/?_sm_au_=i7s4kDQ0LfD1sfNM

Java Threads and Concurrency

http://jlunaquiroga.blogspot.in/2013/04/java-concurrency-part-1.html?_sm_au_=i7s4kDQ0LfD1sfNM

Java Collections

http://jlunaquiroga.blogspot.in/2013/06/java-collections-framework-part-1-intro.html?_sm_au_=i7s4kDQ0LfD1sfNM

Spring

http://javacodebook.com/category/spring/spring-book/?_sm_au_=i7s4kDQ0LfD1sfNM#

How to create SOAP web service client in java with cxf, Spring, Maven

July 9, 2012 Comments off

In continuation with my previous post i am showing a way how to invoke a SOAP service from java.

There are different ways to create a SOAP Clinet in java with cxf.Here im giving the most fastest and easiest way to create.

Get the wsdl URL form the server.

open up the pom.xml file.

Add the below wsdl-to-java plugin.This plugin will generate the java code by parsin the wsdl which is given as input to it.

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://localhost:7009/cxf-spring-test/calculatorService?wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>

now open the commanprompt or terminal,navigate to the project

type mvn clean generate-sources

this will generate the wsdl correspondin code in the <Project Folder>/generated/cxf location.

Add this to the project build path.

now i am writing a simple class and wired to the bean in the application context

Application Context File:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" 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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<!-- Start up bean which will invoke server webservice -->
<bean id="calculatorService" init-method="invoke">
<constructor-arg index="0" value="http://localhost:7009/cxf-spring-test/calculatorService?wsdl"/>
</bean>

</beans>

Java Client Class(As per the above example this will get invoked automatically during start up)

package com.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class CalculatorServiceClient
{
private String targetURL;

public CalculatorServiceClient(String targetURL)
{
this.targetURL = targetURL;
}

public void invoke()
{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(CalculatorIntf.class);

factory.setAddress(this.targetURL);
CalculatorIntf calculatorIntf = (CalculatorIntf) factory.create();

//invoking add web servcie
System.out.println("10 + 15 = "+calculatorIntf.add(10, 15));

//invoking subtract web service
System.out.println("15 - 5 = "+calculatorIntf.subtract(15, 5));

//invoking multiply web service
System.out.println("10 * 5 = "+calculatorIntf.multiply(10,5));
}
}

In the next post i will show how to add SSL Support to the above client code.

Source Code Download : cxf_spring_maven.zip

Categories: Web Services

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

July 7, 2012 2 comments

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.

Categories: Web Services
%d bloggers like this: