RESTful WebService verbraucht XML,wie Sie es nennen?

UPDATE

Habe ich fast in der Lage gewesen, um meine RESTful-Kommunikation, obwohl ich die Fragen bleiben:

1 - Wie ordne ich meine XML für die Verbindung (den code unten ein Beispiel von meiner situation)?

Aufrufen der 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 - Wird die folgende Klasse Ergebnis in einem gültigen XML-Ausgabe, in Anbetracht der letzten code-Beispiel (Web-Service)?

Klasse zu senden, verwenden Erholsamen

@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;
}

InformationsquelleAutor Menno | 2012-06-03

Schreibe einen Kommentar