REST web service accepting a POST using Restlet - Best Practice

13,269

Solution 1

This is more of the kind of response I was looking for. Thanks to Thierry Boileau for the answer:

You can use two kinds of "XML representations": DomRepresentation and SaxRepresentation. You can instantiate both of them with the posted representation. E.g.: DomRepresentation xmlRep = new DomRepresentation(rep);

The DomRepresentation gives you access to the Dom document. The SaxRepresentation allows you to parse the XML doc with your own contentHandler. See the javadocs here 1 and here 2.

  1. http://www.restlet.org/documentation/1.1/api/org/restlet/res​ource/DomRepresentat​ion.html

  2. http://www.restlet.o​rg/documentation/1.1​/api/org/restlet/res​ource/SaxRepresentat​ion.html

Solution 2

We currently do this using RESTeasy, which is an alternative JAX-RS implementation. We use JAXB bindings (annotations) to map between the XML and our model POJOs, and specify a JAXB provider to JAX-RS so it knows how. This is described in our RESTful web services in Java EE with RESTEasy (JAX-RS) article, which may help.

Update: for Restlet, the JAXB extension might be what you need.

Share:
13,269
Vidar
Author by

Vidar

20 years a GIS Developer. I develop mostly C# based applications, WPF and have dabbled with MVC, Blazor technologies. I also tend to use Python to get things done quickly e.g. making changes or extracting data out of Postgres databases.

Updated on June 04, 2022

Comments

  • Vidar
    Vidar almost 2 years

    I have my resource and they typical overridden method to handle POST requests.

    public void acceptRepresentation(Representation rep) {
    
      if (MediaType.APPLICATION_XML.equals(rep.getMediaType())) {
          //Do stuff here
      }
      else {
          //complain!
      }
    }
    

    What I want to know is the best practice to handle my packet of XML. I see a lot of examples using a Form - but surely there is a way to work with the Representation object itself or cast it to some useful XML object???

    Any help on how you should and do parse incoming XML in your resource is much appreciated.