blog

Home / DeveloperSection / Blogs / Fi Techniques for Creating Java Web Services From WSDL

Fi Techniques for Creating Java Web Services From WSDL

AIS Technolabs1180 26-Mar-2019

Fi Techniques for Creating Java Web Services From WSDL

In this article, I will actualize a basic Document/Literal web service, from a current WSDL file, utilizing three unique data binding frameworks and two low-level methodologies. The objective is to think about the Custom Java Development frameworks, mainly from the convenience point of view. For demo purposes, I have picked NetBeans IDE 6.1 with its standard Web Service functionality and its Axis2 plugin. The standard Web Service bolster in NetBeans IDE depends on the JAX-WS stack. These are the frameworks utilized in this article:

JAX-WS 2.1

JAX-WS 2.1

Axis with ADB

AXIOM

Axis with Xmlbeans

WSDL File

At first, I needed to utilize the straightforward AddNumbers.html file from JAX-WS2.1 precedents yet I experienced issues identified with Java artifacts generation with both Axis ADB and Xmlbeans. After some examination, I found the reason: ADB and Xmlbeans have problems creating Java artifacts if (1) the WSDL namespace and pattern namespace are identical and (2) the <wsdl: message> speaking to <wsdl: fault> has the same name from composition component. Even though the WSDL file is WS-I agreeable, the 2 data binding advancements fizzled. I likewise tried JiBX data binding, without progress. To go on I needed to alter the WSDL file a bit: AddNumbers.wsdl (the outline namespace is changed).

Hatchet WS 2.1 (Java Artifacts Generated With wsimport)

For this reason, I've made a new web application venture and utilized a Web Service from WSDL wizard in the Web Services category. The implementation was simple:

@WebService(serviceName = "AddNumbersService", portName = "AddNumbersPort", endpointInterface = "org.example.duke.AddNumbersPortType", targetNamespace = "http://duke.example.org", wsdlLocation = "WEB-INF/wsdl/AddNumbersImpl/AddNumbers.wsdl") publicclass AddNumbersImpl implements AddNumbersPortType {

publicint addNumbers(int arg0, int arg1) throws AddNumbersFault {

 int result = arg0 + arg1;

 if (result < 0) {

  org.example.duke.xsd.AddNumbersFault fault = new org.example.duke.xsd.AddNumbersFault();

  fault.setMessage("the result is negative");

  fault.setFaultInfo("negative result: " + result);

  thrownew AddNumbersFault("error", fault);

 } else {

  return result;

 }

}

publicvoid oneWayInt(int arg0) {

 System.out.println("JAX-WS: oneWayInt request " + arg0);

}

}

The implementation is straightforward, as wsimport, as a matter of course, produces Web Service (SEI class) strategies in the Wrapping Style mode, which implies for solicitations and reactions spoken to by an arrangement of xsd crude sorts, the WS strategy works specifically with Java vulgar sorts. Therefore, JAX-WS utilizes the @javax.xml.ws.RequestWrapper and @javax.xml.ws.ResponseWrapper comments in generated SEI classes. The most unpredictable, yet not troublesome, was the implementation of AddNumbersFault: the particular case is tossed when the outcome is adverse. NetBeans Code Completion helped me enormously here.

JAX-WS 2.1 (Provider API Implementation)

To actualize the low-level methodology supporting by JAX-WS 2.1, I utilized the standard Web Service from WSDL wizard, and I checked the Use Provider checkbox (new NetBeans IDE 6.1 element). The implementation requires one to know the structure of the XML ask for and XML reaction. See that the XML DOM API is utilized to process the demand, while the reaction is composed specially as plain XML content. The hardest part for me was to actualize the Fault correctly.

@ServiceMode(value = javax.xml.ws.Service.Mode.PAYLOAD) @WebServiceProvider(serviceName = "AddNumbersService", portName = "AddNumbersPort", targetNamespace = "http://duke.example.org", wsdlLocation = "WEB-INF/wsdl/AddNumbersImpl/AddNumbers.wsdl") publicclass AddNumbersImpl implements javax.xml.ws.Provider < javax.xml.transform.Source > {

 public javax.xml.transform.Source invoke(javax.xml.transform.Source source) {

  try {

   DOMResult dom = new DOMResult();

   Transformer trans = TransformerFactory.newInstance().newTransformer();

   trans.transform(source, dom);

   Node node = dom.getNode();

   Node root = node.getFirstChild();

   Node first = root.getFirstChild();

   int number1 = Integer.decode(first.getFirstChild().getNodeValue());

   Node second = first.getNextSibling();

   int number2 = Integer.decode(second.getFirstChild().getNodeValue());

   int result = number1 + number2;

   if (result < 0) {

    return getFault(result);

   } else {

    return getResponse(result);

   }

  } catch (Exception e) {

   thrownew RuntimeException("Error in provider endpoint", e);

  }

 }

 private Source getResponse(int result) {

  String body = "<ns:addNumbersResponse xmlns:ns=\"http://duke.example.org/xsd\"><ns:return>" + result + "</ns:return></ns:addNumbersResponse>";

  Source source = new StreamSource(new ByteArrayInputStream(body.getBytes()));

  return source;

 }

 private Source getFault(int result) {

  String body = "<nsf:Fault xmlns:nsf=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<faultcode>nsf:Server</faultcode>" + "<faultstring>error</faultstring>" + "<detail>" + "<ns:AddNumbersFault xmlns:ns=\"http://duke.example.org/xsd\">" + "<ns:faultInfo>negative result " + result + "</ns:faultInfo>" + "<ns:message>the result is negative</ns:message>" + "</ns:AddNumbersFault>" + "</detail>" + "</nsf:Fault>";

  Source source = new StreamSource(new ByteArrayInputStream(body.getBytes()));

  return source;

 }

}

AXIS With ADB

To execute a web service utilizing Axis ADB, I introduced the Axis2 plugin on the highest point of NetBeans IDE 6.1. To set up NetBeans IDE with Axis2 bolster, see the instructional exercise: Creating Apache Axis2 Web Services on NetBeans IDE. To make web service from a WSDL file, I utilized the Axis Web Service from WSDL wizard from the Web Services category. In the wizard, the WSDL file can be chosen in the Name and Location Panel and ADB data binding stack in Code Generator Options board - > Databinding Technology combo box. The Axis2 wsdl4j utility is called from the wizard, and the skeleton class for web service is generated to execute. The implementation is genuinely basic, instinctive, and clear. The code culminations helps a great deal :

publicclass AddNumbersImpl implements AddNumbersServiceSkeletonInterface {

public AddNumbersResponse2 addNumbers(AddNumbers1 addNumbers0) throws AddNumbersFault {

 int result = addNumbers0.getAddNumbers().getArg0() + addNumbers0.getAddNumbers().getArg1();

 if (result < 0) {

  AddNumbersFault fault = new AddNumbersFault();

  AddNumbersFault0 faultMessage = new AddNumbersFault0();

  org.example.duke.xsd.AddNumbersFault fDetail = new org.example.duke.xsd.AddNumbersFault();

  fDetail.setFaultInfo("negative result " + result);

  fDetail.setMessage("the result is negative");

  faultMessage.setAddNumbersFault(fDetail);

  fault.setFaultMessage(faultMessage);

  throw fault;

 } else {

  AddNumbersResponse resp = new AddNumbersResponse();

  resp.set_return(result);

  AddNumbersResponse2 response = new AddNumbersResponse2();

  response.setAddNumbersResponse(resp);

  return response;

 }

}

publicvoid oneWayInt(org.example.duke.xsd.OneWayInt3 oneWayInt2) {

 try {

  OMElement request = oneWayInt2.getOMElement(OneWayInt3.MY_QNAME, OMAbstractFactory.getOMFactory());

  System.out.println("ADB:oneWayInt request: " + request);

 } catch (ADBException ex) {

  ex.printStackTrace();

 }

}

}

Note: Axis2 doesn't utilize the Wrapping Style, so the parameter of the AddNumbers strategy, in the skeleton class, is the AddNumbers1 object rather than 2 int parameters (I didn't discover this if axis2 is empowered to set up a wrapping style).

Adage (AxisObject Model)

This is a low-level procedure, like the JAX-WS Provider/Dispatcher API. Working with OM hubs and components is more comfortable than contrasting them with the DOM however less so than contrasting them with ADB or JAXB. I'd contrast it with working with the SAAJ API. The implementation is additionally straightforward however requires the information of the AXIOM API. The skeleton class can be generated by Axis Service from WSDL wizard by choosing the AXIOM Data Binding Technology.

Once more, I invested the more significant part of the energy managing the Fault implementation:

Pivot With Xmlbeans

This is the WS Stack supporting likewise by Axis2 innovation. The skeleton class can be generated by the Axis Service from WSDL wizard by choosing the Xmlbeans Databinding Technology.

The implementation is obvious and like Axis2 ADB. The clumsiness of this methodology is the number of classes (225) generated by choosing this choice. I am not a specialist in Xmlbeans so this number might be diminished in some way or another. This is the implementation class:

About The Author

Code Wilson is a Marketing Manager at AIS Technolabs which is Web design and Development Company, helping global businesses to grow by Java Web Development Services. I would love to share thoughts on Social Media Marketing Services and Game Design Development etc.


I Hermit Chawla , Marketing Manager at Ais Technolabs which is Web design and Development Company, helping global businesses to grow by node js development company . I would love to share thoughts on Social Media Marketing Services and Game Design Development etc.

Leave Comment

Comments

Liked By