1

Is there a way to pass a list of elements as a parameter to a method using Jersey?

I have the class Sample annotated with @XmlRootElement with the following variables: idUsuario, x, and timestamp.

In my MedicaoResource class, the method:

@GET
@Produces("text/xml")
public List<Medicao> getMedicoes() {
    return new ArrayList<Medicao>(getAllMedicoes());
}

returns something like:

<medicaoes>
    <medicao>
        <idUsuario>1</idUsuario>
        <timestamp>2012-04-16T12:25:00.454-03:00</timestamp>
        <x>0.71</x>
    </medicao>
    <medicao>
        <idUsuario>1</idUsuario>
        <timestamp>2012-04-16T12:25:00.454-03:00</timestamp>
        <x>1.71</x>
    </medicao>
</medicaoes>

Now, I would like to do something like this:

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces("text/plain")
public String addMedicoes(ArrayList<Medicao> medicoes) {
    for (Medicao medicao : medicoes) {
        addMedicao(medicao);
    }
    return "success";
}

How would I have to write my XML for the method above to work properly? I have tried passing the same XML that the method getMedicoes returns, but it won't work.

4

1 回答 1

1

You should use type List<Medicao> instead of ArrayList in the addMedicoes method. This is probably what was preventing jersey/jaxb to properly unmarshall your xml query.

Besides, you should post more information apart from won't work, because with just that, we can only guess what's wrong. What does not work ? Do you have any error message/log to show. What exactly did you try?

于 2012-04-16T20:31:05.713 回答