Java Web Services in NetBeans

The promise of web services is that it would allow seamless communication between disparate systems – that is between systems that make use of different environments like Dot Net and J2EE.  Java has number of open source tools for creation of web services Notable among them is Apache Axis.But support for it is not available in NetBeans6.0 and is forthcoming only in Netbeans 6.1.NetBeans6.0 has support for Glassfish’s implementation JAX-WS and we will a create a web service using it. Java SDK6 has also necessary tools to create a JAX-WS web service.


First of all, what distinguishes web services from other technologies? Earlier this inter-operability between different systems was there, with CORBA, RMI(IIOP) .The major difference between these technologies and the web services is the standardization. Web services are based on standardized XML (as opposed to a proprietary standard) and supported globally by most major technology firms. XML provides a language-neutral way for representing data, and the global corporate support ensures that every major new software technology will have a web services strategy. One such strategy uses SOAP and WSDL as shown in the diagram in Fig-1.

As with EJB, and CORBA, the core abstraction for Web services is a remote procedure call. Web services uses the protocol SOAP, which prescribes the package structure of the XML data exchanged over network The Web Services Definition Language (WSDL) provides a standard specification of a service. But now as a part of the general trend towards simplification, whether it is web application or web services, there are criticisms from some circles, especially from “Rails” votaries, that SOAP-WSDL web services have become unduly complex. Thanks to an influential Ph.D. dissertation by Roy Fielding, which describes the REST (REsource State Transfer) application-networking strategy, now there is another (simpler) way.
 REST is fundamentally different from full-stack Web services for three major reasons:

  • The core abstraction in REST is a remote resource instead of a remote procedure call.
  • Rather than inventing an exhaustive list of standards, REST uses existing Internet standards, including HTTP, XML, and TCP/IP.
  • Instead of every possible scenario, REST covers the most common problems.

Think of REST as browsing. REST clients use the same HTTP commands as your browser to access resources. When a REST client accesses a representation of a resource, the client transitions into a state. With various HTTP commands, REST clients can create, read, update, or delete records from a resource.

In a nutshell, REST:

  • Uses TCP/IP naming standards to name resources on the Web
  • Queries and manipulates those resources with HTTP
  • Uses standard text-based message formats like XML or HTML to structure data

We will see about NetBeans support for RESTful web services later and for the present we will continue with its support for web services based on SOAP and WSDL, as still it is the main-stream web service. Usually a WSDL document is automatically generated by Web services framework tools from the code written in a particular programming language and this WSDL file is made available to Client side The client side tools use these WSDL documents to generate client-side stubs or proxies, which provide convenient access to Web services. Thus the key to enabling seamless Web services interoperability is the ability of one Web services framework to consume the WSDL documents generated by other frameworks. The WSDL interoperability effort is just taking off. In the first place how the client knows about the available web services –by consulting UDDI registries maintained by third parties (brokers) and with whom the service providers got registered themselves.

Coming to Java Web Services, JAX-RPC API was used earlier.  It’s improved version JAX-WS is used presently. JAX-WS relies on JAXB, another API to match between data types in java classes and WSDL files generated from them and vice versa. As mentioned earlier JAX-WS is now a part of Java SE6 also .The effect of inclusion of JAX-WS in Java SE6 is both negative and positive. Formerly only EJB stateless session beans and web applications in a J2EE container can be exposed as web services .Now even Java SE programs attached with annotations can be exposed as web services. For the purpose, the light-weight web server given with Java SDK6 or Tomcat6 can be used. The command line tools, like apt(annotation processing tool) or  wsgen which generate WSDL from ordinary Java classes or vice versa, make easy the creation web services with Java SDK6 .The negative side is version conflicts with existing projects and existing JAX-WS libraries .The JAX-WS version in SDK6 is 2.0 and version available as a Glassfish project is 2.1.When JAX-WS 2.1 used with SDK6, version conflicts may pose problems .If you want to get to know the problems involved and solutions for them ,Rama Pulavarthi’s blog is suggested.

We will create our Sample project in Netbeans6.0 with JAX-WS 2.1 and with Java5   Choose File->New Project ->Web-> Web Application. In the wizard as in Fig-1 You can choose built-in Tomcat web server. The Tomcat that comes with NetBeans comes with necessary libraries .You have to uncheck the check box Set Source Level to 1.4 .You can give the project name Calculator .  

By right clicking on the project you can choose New->Web Service and get the wizard as in Fig-2

Click “Add Operation” in  the wizard as in Fig-3.

You can give Name multiply in place of operation Browse Return Type as int . Choose Parameters and click Add. You will the wizard as in Fig-4

Change the Type to int, Fill up Name as “x” and click ok.  Like wise add another “int” parameter y. Click ok in the Add Operation wizard as in Fig-3 and you will see the code generated by the IDE as in Fig-5

Fig-5

@WebService()
public class calculate {

/**
* Web service operation
*/
@WebMethod(operationName = "multiply")
public int multiply(@WebParam(name = "x")
int x, @WebParam(name = "y")
int y) {
int z = x * y;
return z;
}

}   
}

You can modify the code generated as above .The modification done was
// TODO implement operation  return 0 “    replaced with

int  z = x  * y;
 return z.”

Right-click on Calculator in the project node, and click Run. If you get “Build successful” message Open web services node..Right Click on CalculatorWS and choose Test Web Services. You can get Fig6 in your default browser. Otherwise Type http://localhost:8084/Calculator/calculate You will get Fig6.

If you click on the link you will get the WSDL generated and we will see how the client can use this file to use the web services published in the next part of the article .  








}