google Analytics

Friday, August 13, 2010

WebService with Annotaion Example using JBOSS

=============================================================================================================================================
CalculatorBean.java
=============================================================================================================================================
@Stateless
@WebService(endpointInterface = "org.jboss.tutorial.webservice.bean.Calculator")
public class CalculatorBean
{
public int add(int x, int y)
{
return x + y;
}

public int subtract(int x, int y)
{
return x - y;
}
}

=============================================================================================================================================
Calculator.java
=============================================================================================================================================
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface Calculator extends Remote
{
@WebMethod
int add(int x, int y);

@WebMethod
int subtract(int x, int y);
}
=============================================================================================================================================
Client.java
=============================================================================================================================================
public class Client
{
public static void main(String[] args) throws Exception
{
URL url = new URL("http://localhost:8080/jboss-ejb3-tutorial-webservice/CalculatorBean?wsdl");
QName qname = new QName("http://bean.webservice.tutorial.jboss.org/", "CalculatorBeanService");

ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(url, qname);

Calculator calculator = (Calculator) service.getPort(Calculator.class);

System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.subtract(1, 1));
}
}

No comments:

Post a Comment