1

I am creating a simple RESTful web service with simple types successfully. Now I want to pass an object as argument for web service and get the object as response. My scenario is, Parse the XML message as object by using Jaxb and send the object as request for web service. After that in server side it process the requested object and generates the response xml file and send back it as object.

In URL path i give

 "http://localhost:8080/SampleWS/rest/checkXML/username=visolve&password=visolve" 

for simple type. But in object I don't know how to give the object reference in URL. Please help me how to solve my problem..

Regards

Bathakarai

Bathakarai
  • 1,517
  • 6
  • 23
  • 39
  • I am not sure what purpose it solves. Why not marshal object to xml before invoking the web service and on the other end convert from xml to object. Many frameworks will do this for you inbuilt. You can directly use the xml if you dont want to convert to object. – techuser soma Jul 31 '12 at 14:04
  • Also, any parameters in url are just key value pairs and http will not understand its your object reference or not. – techuser soma Jul 31 '12 at 14:10

2 Answers2

2

Just define a very good-looking domain object. JAXB and JAX-RS will do the rest.

JAXB.

@XmlRootElement
class Regards {

    @XmlElement
    private long sincerely;
}

JAX-RS.

@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@POST
@Path("/sincerely")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response sincerely(final Regards regards) {

    regards.setSincerely(System.currentTimeMillis());

    return Response.ok(regards).build();
}
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1

Though you could certainly include the entire XML content in your URL, I would probably shy away from it.

Think of it this way: if you encode the XML in the URL you're potentially adding more work on both ends. Now the server and client will both need to know how to build the URL properly, and check to make sure everything lines up correctly. What if, in the future, you need to offer a JSON or YAML view of the same content? Now your URL might need to include the content-type as well. What about character-encoding?

All this to say, HTTP provides a terrific transport mechanism which already addresses these concerns. Include the XML as the entity body of the HTTP message, and use the HTTP header to identify what content-type you're sending, character-encoding, etc. This will work both ways (the server and client both can send the XML back/forth), and makes better use of HTTP.

Here's a related link which might help with some of the details. And another.

On a side note, please, please, please tell me you don't plan on sending user-credentials in plain text across an unencrypted link.

Community
  • 1
  • 1
bedwyr
  • 5,774
  • 4
  • 31
  • 49