blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JAX-WS

The same steps follow as in JAX-RS. The major difference in when you generate the client and server code.
The web service runtime selected will be IBM Websphere JAX-WS.

WS server implementation:

@javax.jws.WebService (endpointInterface="com.server.Calculator", targetNamespace="http://server.com/Calculator/", serviceName="Calculator", portName="CalculatorSOAP")
public class CalculatorSOAPImpl{

    public String calculate(CalcRequestType in) {
     //do business operation here or delegate it to a business component
     System.out.println(in.getOperand1());
     System.out.println(in.getOperand2());
     System.out.println(in.getOperation().toString());
     try {
   Thread.sleep(8000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
     if(in.getOperation().equals(OperationType.ADD)){
      return Integer.toString(in.getOperand1()+in.getOperand2());
     }else {
      return Integer.toString(in.getOperand1()-in.getOperand2());
     }
    }

}

The steps for client also is mostly the same except that the runtime selected will be JAX-WS.

WS client call using servlet,


public class TestServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("Called WS");
  CalculatorSOAPProxy proxy = new CalculatorSOAPProxy();
  proxy._getDescriptor().setEndpoint("http://localhost:9089/WS2Server/Calculator");
  CalcRequestType in = new CalcRequestType();
  in.setOperand1(5);
  in.setOperand2(10);
  in.setOperation(OperationType.ADD);
  String result = proxy.calculate(in);
  System.out.println(result);
 }

}

Here is a useful diagram for JAXWS Client API.


After deploying it on the server, we hit http://localhost:9089/WS2Server/Calculator

And the page displays,

{http://server.com/Calculator/}Calculator

Hello! This is an Axis2 Web Service!

Notice that JAX-WS still uses JDNI runtime.

InitialContext ctx = new InitialContext();
_service = (com.server.calculator.Calculator_Service)ctx.lookup("java:comp/env/service/Calculator");

But you wouldn't find anything defined in web.xml. It's because everything is done through annotations.

@WebServiceClient(name = "Calculator", targetNamespace = "http://server.com/Calculator/", wsdlLocation = "WEB-INF/wsdl/Calculator.wsdl")
public class Calculator_Service extends Service
{
  ....
}

No comments:

Post a Comment