views:

5402

answers:

6

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.

A: 

Through the representation.getText() method, you can get a String that can be fed into a SAX parser or dom reader.

Rich Apodaca
A: 

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.

Peter Hilton
Is this anything to do with the Restlet framework?
Vidar
Yes, because Restlet is also a JAX-RS implementation. Sorry I wasn't clear.
Peter Hilton
+2  A: 

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.o​rg/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

Vidar
A: 

@Simon E

I don't understand: which REST implementation for Java are you using?

So, I just give you an example of using JAX-RS (Jersey implementation)

The server part (method of some REST class):

@POST
@Path("/upload")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public static Response upload(
  @FormParam("name") String name,
  @FormParam("content") String content)
  throws Exception {

 // NOTE: you get your content as String
 // (do something here)

 return Response.ok().build();
}

The client part (method of some JUnit test):

@Test
public void uploadFile()
  throws Exception {

 String address = "http://0.0.0.0:8000/r/upload";

 WebResource r = Client.create().resource(address);
 Form form = new Form();
 form.add("name", "test");
 form.add("content", "SOME CONTENT GOES HERE");
 String s = r.post(String.class, form);

 System.out.println(s);
}

That's it !!!

In case you have trouble with imports:
Server needs javax.ws.rs.* and javax.ws.rs.core.*
Client needs com.sun.jersey.api.client.* and com.sun.jersey.api.representation.*

In any way, I would give you the advice to use JAX-RS rather than alternative implementations, because JAX-RS will be part of the upcoming Java EE 6

ivan_ivanovich_ivanoff
I am using Restlet
Vidar
A: 

Is this the same procedure even in restlet 2.0??

I use restlet 2.0m6 and here is the code snippet that I use -

@Post

public Representation process(Representation entity)

{

try

{

DomRepresentation dom = new DomRepresentation(entity);

Document d = dom.getDocument();

.

.

} catch(Exception e)

{ e.printStackTrace(); }

and it throws a Null Pointer exception at the dom.getDocument() line. Which means no data actually arrived.

And my flex bit looks like this - var service : HTTPService = new HTTPService(); service.method="POST"; service.contentType="application/xml" service.url=url; var token :AsyncToken = service.send(params);

where params is an XML object.

Vatsala
A: 

can you please give an example on how you invoke a rest web service with a post and parameters. thanks, roy

roy