Reader small image

You're reading from  Java EE 7 Development with NetBeans 8

Product typeBook
Published inJan 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781783983520
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
David R Heffelfinger
David R Heffelfinger
author image
David R Heffelfinger

David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing, and developing software professionally since 1995, and has been using Java as his primary programming language since 1996. He has worked on many large-scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae, and the US Department of Defense. He also has a Masters degree in Software Engineering from Southern Methodist University. David is Editor-in-Chief of Ensode.net (http://www.ensode.net), a website about Java, Linux, and other technology topics.
Read more about David R Heffelfinger

Right arrow

Chapter 11. SOAP Web Services with JAX-WS

Web Services allow us to develop functionality that can be accessed across a network. What makes web services different from other similar technologies such as EJBs or Remote Method Invocation (RMI) is that they are language and platform independent, for example, a web service developed in Java might be accessed by clients written in other languages and vice versa.

In this chapter, we will cover the following topics:

  • Introduction to web services

  • Creating a simple web service

  • Creating a web service client

  • Exposing EJBs as web services

Introduction to web services


Web services allow us to write functionality that can be accessed across a network in a language- and platform-independent way.

There are two different approaches that are frequently used to develop web services: the first approach is to use the Simple Object Access Protocol (SOAP) and the second approach is to use the Representational State Transfer (REST) protocol. NetBeans supports creating web services using either approach. SOAP web services are covered in this chapter. RESTful web services were covered in the previous chapter.

When using the SOAP protocol, web service operations are defined in an XML document called a Web Services Definition Language (WSDL) file. After creating the WSDL file, an implementation of web services is performed in a proper programming language such as Java. The process of creating a WSDL is complex and error-prone; fortunately, when working with Java EE, a WSDL file can be automatically generated from a web service written in Java...

Creating a simple web service


In this section, we will develop a web service that performs a conversion of units of length. Our web service will have an operation that will convert inches to centimeters and another operation to do the opposite conversion (centimeters to inches).

In order to create a web service, we need to create a new web application project; in our example, the project name is UnitConversion. We can create the web service by right-clicking on our project, going to File | New File, then selecting the Web Services category, and finally selecting Web Service as our file type.

After clicking on Next, we need to enter a name and package for our web service in the following window:

After clicking on Finish, our web service is created. The source code for our web service is automatically opened, as shown in the following screenshot:

As you can see, NetBeans automatically generates a simple "Hello World" web service. The class-level @WebService annotation marks our class as a web...

Exposing EJBs as web services


In our previous web service example, we saw how we can expose a Plain Old Java Object (POJO) as a web service by packaging it in a web application and adding a few annotations to it. This makes it very easy to create web services deployed in a web application.

When working with an EJB module project, we can have stateless session beans exposed as web services. This way, they can be accessed by clients written in languages other than Java. Exposing stateless session beans as web services has the effect of allowing our web services to take advantage of all the features available to EJBs, such as transaction management and aspect oriented programming.

There are two ways of exposing a session bean as a web service. When creating a new web service in an EJB module project, the web service will automatically be implemented as a stateless session bean. Additionally, existing session beans in an EJB module project can be exposed as a web service.

Implementing new web services...

Summary


In this chapter, we explored NetBeans support for SOAP-based web service development using JAX-WS, including how to expose POJO methods as web services and how NetBeans automatically adds the required annotations to our web services.

We covered how NetBeans aids us in creating web service clients by generating most of the required boilerplate code; we just need to initialize any parameters to be passed to our web service's operations.

Additionally, we covered how to expose EJB methods as web service operations and how NetBeans supports and makes it easy to expose both new and existing EJBs as web services.

Finally, we saw how NetBeans can help us implement a web service from an existing WSDL file, which is located either on our local file system or deployed on a server by generating method stubs from said WSDL.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Java EE 7 Development with NetBeans 8
Published in: Jan 2015Publisher: PacktISBN-13: 9781783983520
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
David R Heffelfinger

David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing, and developing software professionally since 1995, and has been using Java as his primary programming language since 1996. He has worked on many large-scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae, and the US Department of Defense. He also has a Masters degree in Software Engineering from Southern Methodist University. David is Editor-in-Chief of Ensode.net (http://www.ensode.net), a website about Java, Linux, and other technology topics.
Read more about David R Heffelfinger