Tuesday, December 6, 2011

JAX-Ws Based Spring Webservices W/o Arguments

Here we are going to create a JAX-Ws based webservices in spring that simply return a message without taking any arguments.

GreetingServiceInter.java
-------------------------
package com.satya;

public interface GreetingServiceInter {
    public String sayHello();
}


GreetingService.java
------------------------
package com.satya;
    
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import org.springframework.stereotype.Service;

@SOAPBinding(parameterStyle=ParameterStyle.BARE)
@WebService(serviceName="GreetingService")
@Service("greetingService")
public class GreetingService implements GreetingServiceInter
{
 public String sayHello()
 {
  return "Hello from Greeting Service";
 }
}
ServiceExposer.java
-----------------------
package com.satya;
import org.springframework.context.support.*;
public class ServiceExposer {
    public static void main(String[] args)
    {
        FileSystemXmlApplicationContext ctx=new FileSystemXmlApplicationContext("MyCfg.xml");
        System.out.println("Service Exposed Successfully ");
    }
}

MyCfg.xml
-------------
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 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:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

 <context:component-scan base-package="com.satya" />
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
                   <property name="baseAddress" value="http://localhost:1234/" />
</bean>
 </beans>



Note:-Run ClientExposer.java to expose your GreetingService.java (sayHello()) as WebService
This set up will use Java WebServer(jdk1.6+ supported),to expose the webservice.
Here we have decided to expose the webservice at port no 1234,

To view WSDL
                             http://localhost:1234/GreetingService?WSDL

***********************************************************************************
                                                          Client Side
***********************************************************************************
GreetingServiceInter.java
--------------------------
package p1;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;

@WebService
@SOAPBinding(parameterStyle=ParameterStyle.BARE)
public interface GreetingServiceInter {
    public String sayHello();
}

ServiceClient.java
-------------------
package p1;
import org.springframework.context.support.*;
public class ServiceClient {
    public static void main(String[] args)
    {
        FileSystemXmlApplicationContext ctx=new FileSystemXmlApplicationContext("ClientCfg.xml");
        GreetingServiceInter test=(GreetingServiceInter)ctx.getBean("gs");
        System.out.println("result is"+test.sayHello());
    }
}
ClientCfg.xml

--------------
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 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:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<bean id="gs" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean"
  p:wsdlDocumentUrl="http://localhost:1234/GreetingService?WSDL"   p:serviceName="GreetingService"
  p:portName="GreetingServicePort"   p:serviceInterface="p1.GreetingServiceInter"
  p:namespaceUri="http://satya.com/"/>
 
  </beans>

********************************PROGRAM-ENDS**********************************
Note:Run ServiceClient.java to access WebService

No comments:

Post a Comment