wwu-pi / md2-framework

MD2 – Model-driven Mobile Development
Apache License 2.0
14 stars 10 forks source link

Back end serializes single-element lists to a single element in JSON instead of array #7

Closed sacnl closed 7 years ago

sacnl commented 11 years ago

This might be expected by clients of /first, but the Android ManyContentProvider is surprised and cannot parse the result, for example {"contact":{"__internalId":"1","familyName":"Mustermann","givenName":"Max","phone":{"__internalId":"2","number":"012345-45321","kind":"0"},"dateOfBirth":"1985-01-01T00:00:00+01:00"}} A client expecting a multi-valued return type (with an arbitrary number of entries, possibly also one), would expect the following: {"contact":[{"__internalId":"1","familyName":"Mustermann","givenName":"Max","phone":{"__internalId":"2","number":"012345-45321","kind":"0"},"dateOfBirth":"1985-01-01T00:00:00+01:00"}]}

sacnl commented 11 years ago

The Android error message is: Can not deserialize instance of .android.models.Contact$Array out of START_OBJECT token

sacnl commented 11 years ago

Explicitly providing a ContextResolver such as the following produces the second kind of output, but also at /first methods.

package de.wwu.md2.contacts.backend.ws;

import javax.ws.rs.Produces;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;

import de.wwu.md2.contacts.backend.models.Contact;

@Provider
@Produces("application/json")
public class JsonJaxbResolver implements ContextResolver<JAXBContext> {

    @Override
    public JAXBContext getContext(Class<?> type) {
        JSONConfiguration.MappedBuilder b = JSONConfiguration.mapped();
        b.arrays("contact");
        try {
            return new JSONJAXBContext(b.build(), Contact.class);
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}