RESTful WebService consumes XML,how to call it?

call, java, rest, web-services

Solution

Try this:

conn.setDoOutput(true);
OutputStream output = conn.getOutputStream();
// And write your xml to output stream.

Check this link for using REST with standard `URL`: http://rest.elkstein.org/2008/02/using-rest-in-java.html

EDIT

First of all, you need to change your `getByAccount` request to `POST` request, because `GET` request doesn't allow to pass any information in body, it uses only request parameters in url. But you send XML, so use `POST`.

Try following version of your send method:

public Person getByAccount(Account account) {   
    URL url = new URL(uri);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept", "application/xml");
    connection.setOutput(true);

    XStream xstream = new XStream();
    xstream.toXML(account, connection.getOutputStream());

    Person person = (Person) xstream.fromXML(connection.getInputStream());   
    connection.disconnect();
    return person;
}

Problem

UPDATE I have almost been able to complete my RESTful communication, though I have to questions remaining: 1 - How do I assign my XML to the connection (the code below will give an example of my situation)? Calling the Web Service ``` public Person getByAccount(Account account) { URL url = new URL(uri); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/xml"); XStream xstream = new XStream(); String xmlIn = xstream.toXML(account); // Put the xmlIn into the connection BufferedReader br = new BufferedReader(new InputStreamReader( (connection.getInputStream()))); StringBuilder sb = new StringBuilder(); String line; while((line = br.readLine()) != null) sb.append(line); String xmlOut = sb.toString(); connection.disconnect(); return (Person) xstream.fromXML(xmlOut); } ``` 2 - Will the class below result in a valid XML-output, considering the last code example (the Web Service)? Class to send using RESTful ``` @XmlRootElement(name="people") public class People { @XmlElement(name="person") public List<Person> people; public People() { people.add(new Person(1, "Jan")); people.add(new Person(2, "Hans")); people.add(new Person(3, "Sjaak")); } public List<Person> all() { return people; } public Person byName(String name) { for(Person person : people) if(person.name.equals(name)) return person; return null; } public void add(Person person) { people.add(person); } public Person update(Person person) { for(int i = 0; i < people.size(); i++) if(person.id == people.get(i).id) { people.set(i, person); return person; } return null; } public void remove(Person person) { people.remove(person); } } ``` Web Service ``` @GET @Path("/byAccount") @Consumes("application/xml") @Produces("application/xml") public Person getByAccount(Account account) { // business logic return person; } ```

Original source