Now Lets us see how to create a Spring WebService,which takes parameters,does sumthing with that and returns the Result.
ServerSide
CalculatorInter.java
-----------------------
package com.satya;
public interface CalculatorInter {
public String sum(int x,int y);
}
CalculatorImpl.java
----------------------
package com.satya;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.springframework.stereotype.Service;
@SOAPBinding(style=SOAPBinding.Style.RPC,use=SOAPBinding.Use.LITERAL,parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
@WebService(serviceName="CalcyWs")
@Service("Calcy")
public class CalculatorImpl implements CalculatorInter {
@WebMethod
public String sum(int x, int y) {
return ""+(x+y);
}
}
ServiceExposer.java
---------------------
package com.satya;
import org.springframework.context.support.*;
public class ServiceExposer {
public static void main(String[] args)
{
FileSystemXmlApplicationContext ctx=new FileSystemXmlApplicationContext("ServerCfg.xml");
System.out.println("Service Exposed successfully");
}
}
ServerCfg.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:5678/" />
</bean>
</beans>
*****************************SERVER-PROG-ENDS*******************************
Note:-Execute ServiceExposer.java to publish our CalculatorImpl.class as webservice
To view WSDL: http://localhost:5678/CalcyWs?WSDL
**********************************Client-Side***************************************
CalculatorInter.java
---------------------
package p1;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public interface CalculatorInter {
public String sum(int x,int y);
}
ServiceClient.java
------------------
package p1;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class ServiceClient {
public static void main(String[] args) throws Exception
{
FileSystemXmlApplicationContext ctx=new FileSystemXmlApplicationContext("ClientCfg.xml");
CalculatorInter obj=(CalculatorInter)ctx.getBean("gs");
System.out.println(obj.sum(15, 10));
}
}
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">
<property name="wsdlDocumentUrl" value="http://localhost:5678/CalcyWs?WSDL"/>
<property name="serviceName" value="CalcyWs"/>
<property name="namespaceUri" value="http://satya.com/"/>
<property name="serviceInterface" value="p1.CalculatorInter"/>
<property name="portName" value="CalculatorImplPort"/>
</bean>
</beans>
************************PROGRAM-ENDS****************************************
Note:Run ServiceClient.java to consume webservice
ServerSide
CalculatorInter.java
-----------------------
package com.satya;
public interface CalculatorInter {
public String sum(int x,int y);
}
CalculatorImpl.java
----------------------
package com.satya;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.springframework.stereotype.Service;
@SOAPBinding(style=SOAPBinding.Style.RPC,use=SOAPBinding.Use.LITERAL,parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
@WebService(serviceName="CalcyWs")
@Service("Calcy")
public class CalculatorImpl implements CalculatorInter {
@WebMethod
public String sum(int x, int y) {
return ""+(x+y);
}
}
ServiceExposer.java
---------------------
package com.satya;
import org.springframework.context.support.*;
public class ServiceExposer {
public static void main(String[] args)
{
FileSystemXmlApplicationContext ctx=new FileSystemXmlApplicationContext("ServerCfg.xml");
System.out.println("Service Exposed successfully");
}
}
ServerCfg.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:5678/" />
</bean>
</beans>
*****************************SERVER-PROG-ENDS*******************************
Note:-Execute ServiceExposer.java to publish our CalculatorImpl.class as webservice
To view WSDL: http://localhost:5678/CalcyWs?WSDL
**********************************Client-Side***************************************
CalculatorInter.java
---------------------
package p1;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public interface CalculatorInter {
public String sum(int x,int y);
}
ServiceClient.java
------------------
package p1;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class ServiceClient {
public static void main(String[] args) throws Exception
{
FileSystemXmlApplicationContext ctx=new FileSystemXmlApplicationContext("ClientCfg.xml");
CalculatorInter obj=(CalculatorInter)ctx.getBean("gs");
System.out.println(obj.sum(15, 10));
}
}
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">
<property name="wsdlDocumentUrl" value="http://localhost:5678/CalcyWs?WSDL"/>
<property name="serviceName" value="CalcyWs"/>
<property name="namespaceUri" value="http://satya.com/"/>
<property name="serviceInterface" value="p1.CalculatorInter"/>
<property name="portName" value="CalculatorImplPort"/>
</bean>
</beans>
************************PROGRAM-ENDS****************************************
Note:Run ServiceClient.java to consume webservice
No comments:
Post a Comment