• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Best approach for consuming RESTful web services

 
Ranch Hand
Posts: 63
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'd like to have some guidance inside this vast world.

I'm seeking to learn how to consume web services through Java applications. More precisely, I'd like to work with the API Endpoints described in this documentation (https://sites.google.com/expertsender.com/api-documentation-v2), related to an Email Marketing platform to which I possess an API key to connect.

Basically, I need to perform GET e POST requests, receiving and sending XML when demanded. I know some Java SE, and I have the following questions:

1) What's the best approach to start learning it, console, desktop or web application?

2) Is it complex to build an XML file from a desktop/web form for sending it along POST requets, using form fields for creating XML nodes?

3) What would be the recommendation for receiving XML and separating from it only the content of nodes of interest to display the results in a HTML <textarea> or Swing JTextbox?

If you have any links for me to read about it, I'd be pleased, so that I could try something with them. I've come across too many things, but considering the amount of suggestions out there, that facilitates getting lost, truly.

Thanks in advance!
 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When testing a RESTful web service, forget HTML forms and text areas for the XML. That's going to send an application/x-www-form-urlencoded or multipart/form-data request, and that may not always be what you want. It's much more work on the server side. Instead, use a proper web client (not a web browser). I always use Postman for this. It lets you specify every part of the request, including the raw body. If you prefer command line, curl is the way to go; it's also available for Windows. PowerShell also has a built-in curl-like tool.

On the server side, if you want to receive XML, the easiest way is to specify that you consume text/xml or another content type that indicates you want XML. Then, create a class that resembles the XML structure, use that as the request type (likewise for the response), and let JAXB take care of the conversion. Most frameworks support this conversion out-of-the-box, although you may need an additional dependency for it these days.
If you want to use JSON instead (which I prefer over XML), you basically do the same but instead use a library like JSON-B or Jackson to do the conversion.
 
Saloon Keeper
Posts: 24558
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I was asked to describe the different between a Web Service and regular web requests it would be as Rob said: a web service shouldn't be using a form. Forms are primarily for GUI interaction, so while you can certainly use one for a web service - or for dual-purpose - it makes non-GUI usage more complex than it needs to be and consumes network resources in doing so.

Note that a Web Serice and a ReSTful web service are not the same thing. ReST implies that you are not maintaining state within the webapp and thus allows different requests in a workflow to be handled by different servers instead of session-based systems where you either must send all requests in the workflow to the same server or at least have the overhead and complexity of setting up a shared-session server cluster. ReST is widely enjoyed because of its ability to provide micro-services, but its original impetus was to make large-demand systems elastic and scalable.

XML is not the favorite interchange format for web services and SOAP even less so. XML is self-descriptive, but the overhead of all those tags in terms of data being sent over the network and the work it takes to parse XML are a drawback. JSON is undoubtedly the most popular form, since it's not only relatively terse, but many clients have JavaScript built in, and that specifically includes AJAX web service requests. I like YAML, since it's even more compact and easy to manually edit as well, but it doesn't get quite as good client support as JSON.

And, of course, you can do vanilla text, CSV and virtually any other format that you want, although the 3 aforementioned data formats are definitely the most popular.
 
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marcos Silvestri wrote:What's the best approach to start learning it, console, desktop or web application?


If your goal is to learn how to consume web services, then just go for a console application. The rest will just get in the way. Otherwise, it depends on what you want to use your application for.

Is it complex to build an XML file from a desktop/web form for sending it along POST requets, using form fields for creating XML nodes?


Not if you use the correct tools. Many beginners start by putting their own XML/JSON messages together. Don't do this. Use a library that will handle all of this for you.

JAX-RS is a specification that says how RESTful web services can be written in Java, and how they can be consumed by a client application written in Java. Because you're only consuming a web service and not writing one, you only need to use the JAX-RS Client API.

JAX-RS is only a specification. It provides the interfaces you can use in your client application, but it also requires an implementation. Eclipse Jersey is a good implementation of the JAX-RS specification. You need the library that is mentioned under Client application on JDK. You can easily install this library in your project if you use Maven.

What would be the recommendation for receiving XML and separating from it only the content of nodes of interest to display the results in a HTML <textarea> or Swing JTextbox?


You need to deserialize the XML messages to plain old Java objects. Again, don't do this yourself. JAX-B is another specification that says how to serialize and deserialize Java objects to and from XML. Jersey provides an implementation for JAX-B as well, not just JAX-RS. The JAX-B API can be found here.

Here's a small example of what JAX-B looks like. Take a look at the "Get multiple messages" example request. A possible response looks like this:


To model this response, you can write the following classes:


The conversion between the XML messages and the Java objects will be done for you automatically when you use the Client class:
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic