Web services in Java SE, Part 4: SOAP with Attachments API for Java

Learn about SAAJ and other advanced Java SE Web service features

1 2 3 4 5 6 Page 6
Page 6 of 6

At this point, doGet() executes Source result = dispatch.invoke(null);, passing null instead of a Source object as an argument because the provider's doGet() method expects to receive its data as a query string. If an exception occurs during the invocation, a catch block outputs the exception information and exits doGet(). Otherwise, the result object's XML content is transformed into a javax.xml.transform.dom.DOMResult object, which is processed via XPath expressions to obtain result data, which is then output.

If you were to run LibraryClient with Listing 9's doGet() method (you'll need to insert --add-module java.xml.ws on the javac and java command lines when using Java SE 9), and if you were to use the same book-related data presented in Part 3, you would observe the following output:

<?xml version="1.0" ?><response>book inserted</response>

<?xml version="1.0" ?><response>book inserted</response>

9781430210450
0201548550

Title: Advanced C+
Author: James O. Coplien
ISBN: 0201548550
Publication Year: 1992
Publisher: Addison Wesley

Title: Beginning Groovy and Grails
Author: Christopher M. Judd
Author: Joseph Faisal Nusairat
Author: James Shingler
ISBN: 9781430210450
Publication Year: 2008
Publisher: Apress

<?xml version="1.0" ?><response>book updated</response>

Title: Advanced C++
Author: James O. Coplien
ISBN: 0201548550
Publication Year: 1992
Publisher: Addison Wesley

<?xml version="1.0" ?><response>book deleted</response>

9781430210450

Conclusion

Now that you've completed this series on Web services in Java SE, I have some homework that will reinforce your understanding of what you've read:

  1. Create a SOAP-based Library Web service that recognizes two operations, expressed via methods void addBook(String isbn, String title) and String getTitle(String isbn). Create a LibraryClient application that invokes addBook() followed by getTitle() to test this Web service.
  2. Create a LibraryClientSAAJ application that uses SAAJ to perform the equivalent of LibraryClient's tasks. Use SOAPMessage's writeTo() method to output each of the request and response messages for the addBook and getTitle operations.
  3. The RESTful Web service described by Part 3's Library class is flawed in that the doDelete() method doesn't notify the client when requested to delete a nonexistent book. How might you modify this method to report this attempt?

You'll find the answers to these exercises in the answers\Library directory of the code archive that accompanies this article. To gain the most benefit from this series, give the exercises an honest try before peeking at the answers.

download
Get the source code for this post's applications. Created by Jeff Friesen for JavaWorld

The following software was used to develop the post's code:

  • 64-bit JDK 9ea+181

The post's code was tested on the following platform(s):

  • JVM on 64-bit Windows 10
1 2 3 4 5 6 Page 6
Page 6 of 6