如何在android上使用MOXy JSON序列化器/解串器作为REST客户端?

时间:2014-04-04 10:55:44

标签: android json rest jackson moxy

如何在Android上将MOXy JSON序列化器/反序列化器作为REST客户端?

我尝试过EclipseLink和Jersey客户端,但没有成功。

杰克逊 resteasy-mobile ,效果很好,但我有泽西服务器 MOXy ,杰克逊和MOXy有一些冲突,如Map<Key, Value>

MOXY:

"map":{
    "entry":[
        {
            "key":{Key...},
            "value":{Value...}
        },
        {
            "key":{Key...},
            "value":{Value...}
        },
        {
            "key":{Key...},
            "value":{Value...}
        },
        {
            "key":{Key...},
            "value":{Value...}
        },
    ]
}

杰克逊:

"map": {
    "Key.toString()" : {Value...},
    "Key.toString()" : {Value...},
    "Key.toString()" : {Value...},
    "Key.toString()" : {Value...},
}

我尝试过EclipseLink:

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            JAXBContext jc = JAXBContext.newInstance(Trip.class);

            Unmarshaller unmarshaller = jc.createUnmarshaller();
            unmarshaller.setProperty("eclipselink.media-type", "application/json");

            unmarshaller.setProperty("eclipselink.json.include-root", false);
            StreamSource source = new StreamSource("my.rest.url/transport/getTrip/ID");
            JAXBElement<Trip> jaxbElement = unmarshaller.unmarshal(source, Trip.class);

            System.out.println(jaxbElement.getValue().getId());

            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
            marshaller.setProperty("eclipselink.json.include-root", false);
            marshaller.marshal(jaxbElement, System.out);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }
}.execute();

maven依赖项:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.moxy</artifactId>
    <version>2.5.1</version>
</dependency>

我无法使用它,因为这需要来自javax.xml.streamjavax.xml.bind的类文件,这些类文件无法在android中覆盖。我试过JarJar:

rule javax.xml.stream.** hu.fehergeri13.javax.xml.stream.@1
rule javax.xml.bind.** hu.fehergeri13.javax.xml.bind.@1
rule javax.xml.namespace.** hu.fehergeri13.javax.xml.namespace.@1
rule javax.xml.XMLConstants hu.fehergeri13.javax.xml.XMLConstants
rule javax.persistence.** hu.fehergeri13.javax.persistence.@1

但它需要这么多包,它总是以classNotFound异常结束。

我也尝试了泽西客户端但遇到了同样的问题。

new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground(Void... params) {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.register(MOXyJsonProvider.class);

        Client client = ClientBuilder.newClient(clientConfig);

        URI uri = UriBuilder.fromUri("http://my.rest.url/").build();
        System.out.println(uri.toString());

        WebTarget target = client.target(uri.toString());

        TransportSvc resource = WebResourceFactory.newResource(TransportSvc.class, target);

        Trip trip = resource.getTrip("B078821013");
        System.out.println(trip);

        return null;
    }
}.execute();

0 个答案:

没有答案